Scotty Allen Scotty Allen - 7 months ago 26
Bash Question

How can I reverse the order of lines in a file?

I'd like to reverse the order of lines in a text file (or stdin), preserving the contents of each line.

So, i.e., starting with:

foo
bar
baz


I'd like to end up with

baz
bar
foo


Is there a standard UNIX commandline utility for this?

Answer

BSD tail:

tail -r myfile.txt

Reference: FreeBSD, NetBSD, OpenBSD and OS X manual pages.