cat displays the contents of a file to screen. It can also display multiple files concatenated together, and using the shell, its standard output can be redirected from the screen to the file.

Some useful options are:

-n line numbering
-s squeeze sequences of blank lines into one
-v show non-printing characters

Examples

$ cat example.txt
The contents of the file
example.txt are now
displayed.

$ cat -n example.txt
1 The contents of the file
2 example.txt are now
3 displayed.

Concatenating multiple files (in this example, the same file twice):

$ cat example.txt example.txt
The contents of the file
example.txt are now
displayed.
The contents of the file
example.txt are now
displayed.

Concatenate the files, but use the shell to redirect the result to a new file.

$ cat example.txt example.txt > double.txt

We can concatenate binary files, too. Some programs limit downloads to 2 GB; this is bad for larger files like some DVD images. Suppose a web site helpfully divides the file "sodalinux.dvd" into three parts, 2 GB or less, for downloading and later concatentation. We combine the files, and use the > shell redirection output to put the DVD image in a file:

$ cat sodalinux.dvd1 sodalinux.dvd2 sodalinux.dvd3 > sodalinux.dvd

If we want to type less, then most shells also allow this:

$ cat sodalinux.dvd{1,2,3} > sodalinux.dvd

The -v option is useful for viewing control characters embedded mostly in text. In this example, the file "/usr/share/man/cat1/pax.0" is mostly text but contains control characters which the pagers "less" and "more" use to make text bold. Using cat -v we can see the control characters. Here are the first four lines:

Using "cat" with no arguments makes it copy standard input to standard output. Combined with shell redirection, this makes it easy to write a very short text file. All one needs to know is to press Control-D (^D) to indicate end of input, finish the file, and return to the shell. Here is how to write "example.txt":

$ cat > example.txt
The contents of the file
example.txt are now
displayed.
^D $

If you put "cat" with no arguments in a pipe, it only copies standard input to standard output. This might seem useless. For example, the following two pipes have the same function:

$ dmesg | less
$ dmesg | cat | less

However, "cat" can be used as insulation to make programs think that they are not running on terminals. In the next example, GNU bc does not print its copyright message on startup. We enter one calculation ("3 + 9") and then quit (^D):

more paginates output. The problem with "cat" is that if a file is too long, then it falls beyond the top of the screen. The job of "more" is to stop and wait when it fills the screen. Most users find it easier to use "less", but on some systems "more" has all of the features of "less".

Tips: The -f option displays the tail, then waits for and displays any new options to the file. This is normally used to watch log files. (The next example has only three lines from tail, but the 80-column terminal was too narrow, so the lines were broken into five lines.)