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

Scotty Allen picture Scotty Allen · Apr 12, 2009 · Viewed 359k times · Source

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

Mihai Limbășan picture Mihai Limbășan · Apr 12, 2009

Also worth mentioning: tac (the, ahem, reverse of cat). Part of coreutils.

Flipping one file into another

tac a.txt > b.txt