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?

  • (1) Your question is puzzling, you say "since a month" (now edited to "during the past month"), and it is currently November, but you're grepping for "Jul".  (2) I don't understand why you're saying -B 10; please try to explain that better.  (3) "It doesn't seem to be working" is very vague.  What does it do?  (4) Have you tried breaking it down into pieces?  For example, do last | awk '{print $1, $4, $5, $6}' and see whether the output is what you expect, and then work from there. – G-Man Nov 9 '15 at 0:33
  • Did you look at last -t ? – Vinz Nov 9 '15 at 0:33
  • Does last show any entries for July? – muru Nov 9 '15 at 0:34

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


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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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