Bash – display users that logged in since a date using last

awkbashgrepsedshell

I am trying to display the user name of the accounts that have logged in during the past month using the last command.

At the moment I have this

last | awk '{print $1, $4 ,$5 ,$6}' | grep -B 10 Jul | sort -u -t' ' -k1,1

As the users who have logged in most recently are displayed higher in the list, I am trying to grep for the month, then display that line and the lines above it and delete duplicate usernames. But it doesn't seem to be working. Any ideas?

Best Answer

Assuming you are using the version of last in the util-linux package:

last -s '2015-11-01'  | sort -k1,1 -u

or even:

last -s '-1 month'  | sort -k1,1 -u

or

last -s '2015-07-01' -t '2015-07-31' | sort -k1,1 -u

last has -s (--since) and -t (--until) options for restricting output to certain dates and times. The sort -k1,1 -u does a unique sort on just the first field of last's output.

See man last, especially search for TIME FORMATS for more details.

Related Question