The Coreutils cat Command

cat is a command that reads one or more files, concatenates them, and writes them to standard output (stdout), which by default prints the contents to the screen.

This command uses the call signature:

cat {options} {paths}

If no path is specified, then this command receives input from stdin. If one path is specified, then this file is read and passed to stdin. If multiple paths are specified, then they are each read in sequence and passed to stdin.

Basic Usage

As a quick review, lets start with a file fruits.txt with the following contents:

apple

banana

watermelon

grape

strawberry

We can print the contents to the screen using the following command:

·
·
·
ninja$:·cat·fruits.txt
apple
banana
watermelon
grape
strawberry
ninja$:··

Now, suppose we have a second file animals.txt with the following content:

bear

chicken

duck

We can concatenate the files and print them to the screen with the following command:

ninja$:·cat·animals.txt·fruits.txt
bear
chicken
duck
apple
banana
watermelon
grape
strawberry
ninja$:··

Options

Although most invocations of the cat command use the simple call signature, there are a few useful options available.

Adding line numbers

cat --number (or cat -n for short) returns each line prefixed with a line number, which can be handy in some cases:

·
·
·
ninja$:·cat·--number·fruits.txt
1·apple
2·banana
3·watermelon
4·grape
5·strawberry
ninja$:··

Squeezing empty lines

The cat command also provides an option for "squeezing" multiple blank lines down to a single blank line. For example, given a file that contains various blank lines:

one

·

two

·

·

·

three

this option can be used to:

·
·
·
ninja$:·cat·--squeeze-blank·blanks.txt
one
·
two
·
three
ninja$:··