Skip to Content
Shell Scripting: Expert Recipes for Linux, Bash, and More
book

Shell Scripting: Expert Recipes for Linux, Bash, and More

by Steve Parker
August 2011
Beginner to intermediate
600 pages
14h 29m
English
Wrox
Content preview from Shell Scripting: Expert Recipes for Linux, Bash, and More

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 ) > tempfilecat tempfile
Hotel
Golf
Foxtrot
Echo
Delta
Bravo
Alpha
$ tac tempfile > alpha.txtcat alpha.txt
Alpha
Bravo
Delta
Echo
Foxtrot
Golf
Hotel
$ rm tempfile
$
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Start your free trial

You might also like

Linux Command Line and Shell Scripting Techniques

Linux Command Line and Shell Scripting Techniques

Vedran Dakic, Jasmin Redzepagic
Linux Shell Scripting Cookbook - Third Edition

Linux Shell Scripting Cookbook - Third Edition

Clif Flynt, Sarath Lakshman, Shantanu Tushar

Publisher Resources

ISBN: 9781118166321Purchase bookDownloads