Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

I want to fetch user's login time and login date, is there any command in Unix providing User's login date and User's login time ? this problem i want to perform in Shell-script where username is accepting from the end-user and after checking the availability of user, i would like to fetch that user's login time and login date in different variable and then display using 'echo' command.

share|improve this question

2 Answers 2

up vote 18 down vote accepted

For past logins:

last "$USER_NAME"

Also, the command who lists current logins.

If you're looking for the date of the user's last login, some systems provide it directly, for example lastlog -u "$USER_NAME" on Linux or lastlogin "$USER_NAME" on FreeBSD. It's also available in the output of finger, but not in an easy-to-parse form. In any case, it's available in the output of last (on many unix variants, last -n 1 "$USER_NAME" shows the last login; otherwise you can do last "$USER_NAME" | head -n 1). Note that last login may not correspond to the last logout (e.g. a user remained connected from one origin for a long time and made a quick network login recently).

share|improve this answer
    
i know 'who' command is providing the login information of all users , but i wants to have "Login-date" and "Login-time", and i dont wants the user information for past login but for a username accepted from a user and want to fetch the information about that user –  Paresh Mayani Sep 14 '10 at 14:03
    
? last "$username" provides "Login-date" and "Login-time". The second part of your sentence is not clear for me, maybe its my English, maybe its yours... "Username accepted from a user[...]" ? –  echox Sep 14 '10 at 17:13
    
@Paresh: I've run out of ways to interpret your question. If my answer still doesn't help, try making your question clearer. –  Gilles Sep 14 '10 at 17:34
    
@Paresh : last $USER_NAME will show current users as well. It will show something like username ttyp0 192.168.1.100 Tue Sep 13 13:09 still logged in –  Stefan Lasiewski Sep 14 '10 at 22:12
    
@Gilies let me clear what i want to perform actually, i want to make a shell-script in which i accept user-name as "read $username" and then i want to check whether the user is log-on or not , if yes then i want to fetch their login-time and login-date, thanx for the support –  Paresh Mayani Sep 15 '10 at 5:47

On linux, last -R $username | awk '/still logged in/ {print $3,$4,$5,$6}' will return nothing if the user is not logged in, otherwise a date/time list for each active session.

Other unixes that don't know the -R option to last (which supresses the hostname) will need some modification.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.