Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I know that the command last | tac is enough, but I want to do it using the sort command. I cannot sort it by column, it always sorts the first one only. Using bash on Arch Linux.

share|improve this question
up vote 5 down vote accepted

Looks like you can't rely on fields, so you'd need to rely on character column

         1         2         3         4         5         6         7         
stephane pts/0        :0               Fri Aug  1 09:48 - 14:34 (17+04:45)
stephane pts/13       :0               Fri Aug  1 16:27 - 13:51 (20+21:24)

From that:

last | sort -k1.44,1.46M -k1.48,1.49n -k51

Note that the M flag to sort on month names is not standard but available in several sort implementations including GNU sort (the one typically found on ArchLinux). Note that sort interprets the month names in the current locale, while last always outputs English month names, so you may want to run sort under LC_TIME=C if in a non-English locale.

share|improve this answer
The M flag in sort (compare as months) is available on Sun OS/Solaris too. – Dimitre Radoulov Aug 26 '14 at 12:28
@DimitreRadoulov, indeed, it's even in HP/UX, so it's probably not a GNU extension as I thought it was. I've changed the wording. – Stéphane Chazelas Aug 26 '14 at 12:33

Your Answer


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.