2

Example history:

$ history
1  whoami
2  pwd
3  ls

To get a reversed history list, I do:

$ history|tac
3  ls
2  pwd
1  whoami

But are there any better ways to do this, perhaps that needn't invoke another program, for those without tac installed, for example?

  • 2
    tac is from coreutils and should be available on almost every system. – scai Jun 19 '17 at 10:35
  • As scai says + the solution with tac is pretty simple and elegant. But you could also try to use sed '1!G;h;$!d' to reverse the file. Or Perl, Or AWK. – Quora Feans Jun 19 '17 at 10:36
  • 1
    @scai On almost every Linux system. It isn't a traditional Unix utility, it's a GNU extra (also available in BusyBox). – Gilles Jun 20 '17 at 22:21
2

Since the owner of a separate answer deleted it, I'll suggest:

history | sort -rn
1

To list history in reverse order add -rl to fc command:

fc -rl

Obviously fc -l list in standard order. You can also specify the number of entries you want to see e.g. fc -rl -10.

  • This works, but it removes my HISTTIMEFORMAT from every line. – EmmaV Jun 19 '17 at 10:51

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.