cat Backwards is tac
Another useful utility, inspired by cat, is tac. This is yet another play on words along the same lines as yacc, bash, GNU, and many other such self-consciously weak jokes.
$ tac file1 file2
the second is blank.
it has four lines.
this is file1
of which this is the sixth.
it has six lines (three blank)
this is file2
$
Although reversing the order of a file is not often useful, it can be useful in more subtle ways. It is easy to append data to a file, but to prepend data, tac can be very handy. Note that ( tac alpha.txt ; echo Bravo ; echo Alpha ) has to be done in a subshell so that the output of all three commands is written to tempfile.
$ cat alpha.txt Delta Echo Foxtrot Golf Hotel $ ( tac alpha.txt ; echo Bravo ; echo Alpha ) > tempfile $ cat tempfile Hotel Golf Foxtrot Echo Delta Bravo Alpha $ tac tempfile > alpha.txt $ cat alpha.txt Alpha Bravo Delta Echo Foxtrot Golf Hotel $ rm tempfile $
Get Shell Scripting: Expert Recipes for Linux, Bash, and More now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.