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:
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:
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:
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: