The cat command concatenates files or standard input to standard out by default. What this means is that the cat command will display the contents of a file to standard out so you can read it. The cat command is also very useful with redirection allowing you to combine multiple files together as, for example, with the split command. Recall that the split command divided a file into multiple smaller files. To restore the original file the cat command was used to cancatenate the smaller files back into one large file.
The usage of the cat command is quite simple. For instance, if we had a file called cat_test.txt with the following contents:
here is a cat test
I am adding a tab on the next line
there is a tab here
throw some blank lines in here
See how that goes
Issuing:
cat cat_test.txt
Will output the contents of the file to standard out:
here is a cat test
I am adding a tab on the next line
there is a tab here
throw some blank lines in here
See how that goes
There are a few switches that allow you to control the output of the contents. The -n, or --number, will number each line of output:
cat -n cat_test.txt
Produces:
You can just number only the non-blank lines using the -b, or --number-nonblank, switch:
cat -b cat_test.txt
Produces:
The -s, or --squeeze-blank, switch will remove repeated empty lines of output. This switch will not remove all blank lines, but only the second or more blank lines in a row. So in the example file the blank line after the fourth line is retained; but lines 6, 7, and 8 are “squeezed” out:
cat -sn cat_test.txt
Produces:
To see the non-printing characters that are in a file use the -v, --show-nonprinting switch. The example file has been augmented with some dos non-printing characters. Catting the file now:
cat cat_test.txt
Produces
But:
cat -vn cat_test.txt
Shows the non-printing characters:
Note the ^M and ^V characters on lines 10 and 11 respectively.
The -E, or --show-ends, displays a “$” at the end of each line:
cat -E cat_test.txt
Produces:
To show tabs in a file use the -T, or --show-tabs, switch and tabs are replaced with “^I”:
cat -T cat_test.txt
Produces:
There are a few aggregate switches that combine options:
- -e combines -vE ( --show-nonprinting and --show-ends)
- -t combines -vT ( --show-nonprinting and --show-tabs)
- -a combines -vET ( --show-nonprinting, --show-ends, and --show-tabs)
If no file is specified with the cat command standard input is used. Similarly, using a “-” will signify to use standard input instead of a file:
cat
Issued like this by itself the cat command will echo back what is entered through standard input once the “enter” or “return” key is pressed.
The cat command has no pager functionality so all content that the command displays will scroll off the screen. Using a pipe, “|”, output from the cat command can be passed to a pager like less or more:
cat httpd.log | less
The cat command is a handy utility for viewing the contents of a file or redirecting the contents of one file into another. One example, culled from the Linux from Scratch documentation is to use cat as a quick and dirty text editor:
cat > test.sh << “EOF”
This example cats all standard in to test.sh until a single line containing “EOF” is listed. At which point cat closes the file and exits. Using cat like this or with other commands like cut, head, and tail one can perform some pretty sophisticated text manipulation.
Bibliography
- man cat
- info cat
If the video is not clear enough view it off the YouTube website and select size 2 or full screen. Or download the video in Ogg Theora format:
Thank you very much!
- Ogg file hosted by Hacker Public Radio
- Speex file hosted by Hacker Public Radio
- Mp3 file hosted by Hacker Public Radio