To learn last login date

Hello,

[root@server ~]# last| grep -v "root" | head -1
evam     pts/4        10.16.241.217    Fri Nov 20 10:48    gone - no logout

I can see last date without year. But I want to learn year of the last login.

[root@server ~]# last| grep -v "root" | head -1 | awk '{print$4" "$5" "$6" "$7 }'
Fri Nov 20 10:48

How can I understand year of last login?

Best regards,

Please, try last -F

thanks dud.

#last -F working on Linux but it doesn't work on Solaris, HP-UX and IBM-AIX

# last -F| grep -v "root" | head -1 | awk '{print$4" "$5" "$6" "$7" "$8}'
Thu Nov 19 11:27:54 2015

Is there any alternative solution?

*nix timestamps are printed with time but without year if occurred within the last 6 months. Beyond that, year replaces the time.

SysV Unix does not store the year in wtmp.
Most if not all Unix vendors have migrated to own extended wtmp files, named wtmpx or wtmps.
When adapting the last command, some vendors added new non-standard options.
Here is a portable script that makes some guesses and tries to compute the missing year:

last | PATH=/usr/xpg4/bin:/bin:/usr/bin awk '
BEGIN {
m["Jan"]=1
m["Feb"]=2
m["Mar"]=3
m["Apr"]=4
m["May"]=5
m["Jun"]=6
m["Jul"]=7
m["Aug"]=8
m["Sep"]=9
m["Oct"]=10
m["Nov"]=11
m["Dec"]=12
}
{
for (i=4;i<NF;i++) {
  if (($(i-1) in m) && $i~/^[0-9]?[0-9]$/ && $(i+1)~/^[0-9]/) {
    if ((m[$(i-1)]>pm || m[$(i-1)]==pm && $i>pday) && NR>1) year--
    pm=m[$(i-1)]; pday=$i
    $i=$i " " year
    break
  }
}
print
}' year=`date +%Y`
1 Like

Solaris:

lastlog