How to reverse lines of a text file?

In GNU coreutils, there's tac(1)


There is a command for your purpose:

tail -r file.txt
  • Prints the lines of file.txt in reverse order!
  • The -r flag is non-standard, may not work on all systems, works e.g. on macOS.
  • Beware: Amount of lines limited. Works mostly, but when working with huge files be careful and check.

Answer is not 42 but tac.

Edit: Slower but more memory consuming using sed

sed 'x;1!H;$!d;x'

and even longer

perl -e'print reverse<>'