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.
2 Answers
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).
-
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 Sep 14, 2010 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[...]" ?– echoxSep 14, 2010 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. Sep 14, 2010 at 17:34
-
@Paresh :
last $USER_NAME
will show current users as well. It will show something likeusername ttyp0 192.168.1.100 Tue Sep 13 13:09 still logged in
Sep 14, 2010 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 Sep 15, 2010 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.