Tip: Concatenating files with cat
Get to know your textutils
Often you need to process several files as one and
save the results of such processing to a single output file. The
cat (short for "concatenate") command takes one or more
files on its input and prints them to its output as a single file. For example,
cat chapter01 chapter02 chapter03 > book saves
chapterXX files into a single
The input files are printed in the order they are listed after the
cat command, so to reverse the order of information,
you must first reverse the order of input files. Also, when the number of
files that need to be processed is too large for you to type their names by hand,
you may use a wildcard, as in
cat chapter* >
book remembering that the names of the files will be sorted in
ascending order. This may cause interesting problems when you
suddenly find out that
chapter13 is sent to the
chapter13 is sent to the output after
When the output of
cat is not redirected to a
file or another command's standard input,
behaves as most command-line tools and send its output to the console.
This means that you can use
cat to display
files; for example, you can use
cat /etc/passwd to display the
contents of the system password file. For convenience, should should view large files with
less, as in
less /etc/passwd (you can learn more about
less by typing
cat is primarily used to concatenate
files, you can also use it for simple automatic processing of input. For
example, you can remove multiple blank lines with a single blank line (use
-s option), which is a good way to clean up
source code before showing it to the world. Unfortunately,
cat doesn't have an option for removing blank lines
altogether. But that's not a big problem, because you can remove them
with the handy
Listing 1. Using sed with cat to remove blank lines
$ cat -s /etc/X11/XF86Config | sed '/^[[:space:]]*$/d' ... # Multiple FontPath entries are allowed (they are concatenated together) # By default, Red Hat 6.0 and later now use a font server independent of # the X server to render fonts. FontPath "/usr/X11R6/lib/X11/fonts/TrueType" FontPath "unix/:7100" EndSection ...
Blank line compression is a handy trick for reading config files and sources of HTML pages, especially those generated by scripts that insert gratuitous newlines, as well as sources that contain large conditional structures whose items have been separated with empty lines.
Another important feature of
cat is its ability
to number lines. This functionality comes in handy for program
documentation as well as legal and scientific documentation. Line numbers
printed on the left side make it easy to refer to a certain part of
the document. This is very important in programming, scientific research,
business reporting, or even legislative work. There are two options for
-b for numbering non-blank
lines only and
-n for numbering all lines:
Listing 2. Numbering lines
$ cat -b /etc/X11/XF86Config ... 14 # Multiple FontPath entries are allowed (they are concatenated together) 15 # By default, Red Hat 6.0 and later now use a font server independent of 16 # the X server to render fonts. 17 FontPath "/usr/X11R6/lib/X11/fonts/TrueType" 18 FontPath "unix/:7100" 19 EndSection ... $ cat -n /etc/X11/XF86Config ... 20 # Multiple FontPath entries are allowed (they are concatenated together) 21 # By default, Red Hat 6.0 and later now use a font server independent of 22 # the X server to render fonts. 23 24 FontPath "/usr/X11R6/lib/X11/fonts/TrueType" 25 FontPath "unix/:7100" 26 27 EndSection ...
cat can also help when you are viewing files
that contain non-printing character like tabs. You can reveal them with
-Tdisplay tabs as
-vdisplays non-printing characters, except line-feed and tab using their "control sequence" equivalents. For example, when you process a file generated on a Windows system, it will use Control-M (
^M) characters to mark the end of the line. Characters with codes higher than 127 will be preceded with
M-as in "meta", which is an equivalent of
Alt-characters on other systems.
-Eadds the dollar sign (
$) at the end of each line.
Listing 3. Displaying non-printing characters
$ cat -t /etc/X11/XF86Config ... # Multiple FontPath entries are allowed (they are concatenated together) # By default, Red Hat 6.0 and later now use a font server independent of # the X server to render fonts. ^IFontPath^I"/usr/X11R6/lib/X11/fonts/TrueType" ^IFontPath^I"unix/:7100" EndSection ... $ cat -E /etc/X11/XF86Config ... # Multiple FontPath entries are allowed (they are concatenated together)$ # By default, Red Hat 6.0 and later now use a font server independent of$ # the X server to render fonts.$ $ FontPath "/usr/X11R6/lib/X11/fonts/TrueType"$ FontPath "unix/:7100"$ $ EndSection$ ... $ cat -v /etc/X11/XF86Config ... ^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@M-|M-8^X^@^@^@ P^@^O"M-X^O M-@^M^@^@^@M-^@^O"M-@M-k^@M-8*^@ @M-^H$M-@M-9|A(M-@)M-yM-|M-sM-*M-hW^A^@^@j^@ M-|M-sM-%1M-@M-9^@^B^@^@M-sM-+fM-^A= ^@ ^@ F^@^@ ^@M-9^@^H^@^@M-sM-$M-G^E(l!M-@M-^? ^IM-A5^@^@^D^@PM-^]M-^\X1M-H%^@^@^D^@tyM-G ...
Next time, we'll take a look at
tail. See you then! In the meantime, if you have questions or comments, I'd love to hear from you -- send e-mail to
- Find even more info on these tools we know and love as textutils in the GNU text utilities manual (an expanded view of the same TOC lives at MIT, where you can also find a great list of even more useful GNU tools).
- Windows users can find these tools in the Cygwin package.
- Mac OS X users may want to try Fink, which installs a rich UNIX environment under the Mac OS X.
- The classic work in this field is UNIX Power Tools, by Jerry Peek, Tim O'Reilly, and Mike Loukides (O'Reilly and Associates, 1997); ISBN 1-56592-260-3.