5 UNIX / Linux cat Command Examples

What is cat?
5 cat examples
Syntax and Options
Related Commands

What is cat?

The cat command is used to print the contents of a file on standard output. If more than one files are given as arguments then this command concatenates the contents and produces the output. If no argument is provided then cat expects user to provide the contents through standard input.

5 cat Examples

1. Print the contents on standard output

This is the most common usage of cat command. To quickly print the contents of a file on standard output just supply the file name as an argument to this command.

Consider the following example :

$ cat player.txt
john 1
peeter 2
mary 8
00000000000000000000 0

So we see that the contents of the file player.txt were displayed on standard output.

2. Number lines in output using -n option

If its desired that the content of files (displayed by cat) should contain line numbers then the option -n can be used for this purpose.

Consider the following example :

$ cat -n player.txt
     1	john 1
     2	peeter 2
     3	mary 8
     4	00000000000000000000 0

So we see that the line numbers were printed along with the file content in the output.

3. Number only non blank lines using -b option

If its required that only non empty lines to be numbered then the option -b can be used for this purpose.

Consider the following example :

$ cat -b player.txt
     1	john 1
     2	peeter 2
     3	mary 8
     4	00000000000000000000 0

     5	Mark

So we see that only non empty lines were numbered in the output.

4. Display $ at the end of each line using -E option

Sometimes a line which may seem empty may contain some invisible control characters. In order to be sure of each line termination an option -E can be used which displays ‘$’ sign at the end of each line.

Consider the following example :

$ cat -E player.txt
john 1$
peeter 2$
mary 8$
00000000000000000000 0$
$
$
Mark$

So we see that the line termination character was displayed at the end of each line.

5. Suppress repeated empty output lines using -s option

If repeated empty lines in the output are needed to be suppressed then -s option is used for this purpose.

Consider the following example :

$ cat -s player.txt
john 1
peeter 2
mary 8
00000000000000000000 0

Mark

So we see that two empty output lines were suppressed to one using -s option.

Syntax and Options

cat [OPTION]... [FILE]...
Short Option Long Option Option Description
-A –show-all equivalent to -vET
-b –number-nonblank number non empty output lines
-E –show-ends display $ at end of each line
-n –number number all output lines
-c –changes like verbose but report only when a change is made
-s –squeeze-blank suppress repeated empty output lines
-T –show-tabs display TAB characters as ^I
-v –show-nonprinting use ^ and M- notation, except for LFD and TAB

Related Commands