Linux Cat Command Explained With Examples

By | 15/08/2013

Cat command is one of the popular commands that is frequently used in day-to-day work on Linux. Though it is largely used to display contents of a file on standard output but it can also be used to produce concatenated files. In this article, we will discuss cat command in Linux through some practical examples.

NOTE – All the examples presented in this article are tested with bash shell on Ubuntu 13.04

 

Cat command examples in Linux

1. Display file contents

In its very basic form, cat command can be used to display contents of a file.

Here is an example :

$ cat output 
Old Text is overwritten
Old Text is not overwritten now

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

2. Number lines through -n option

All the lines of the file output can be numbered using -n option of cat command.

Here is an example :

$ cat -n output 
     1	Old Text is overwritten
     2	Old Text is not overwritten now

So we see that the numbered lines were displayed in the output.

3. Number only non blank lines using -b option

If file contains empty lines then you can skip numbering empty lines through -b option.

Here is an example :

$ cat -b output 
     1	Old Text is overwritten
     2	Old Text is not overwritten now

     3	A line after an empty line.

So we see that the empty line was not numbered.

This option (-b) overrides -n option discussed earlier. This means that even if -n is specified along with -b, empty lines will not be numbered in the output.

Here is an example :

$ cat -b -n output 
     1	Old Text is overwritten
     2	Old Text is not overwritten now

     3	A line after an empty line.

So we see that empty lines were not numbered even if -n was used.

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

End of every line of the file can be demarcated using -E option.

Here is an example :

$ cat -E output 
Old Text is overwritten$
Old Text is not overwritten now$
$
A line after an empty line.$

So we see that a $ sign was placed at the end of each line in output.

5. Display tab characters as ^I using -T option

If file contains tabs and it is required to know the presence of tabs then -T option can be used.

Here is an example :

$ cat -T output 
Old Text is overwritten
Old Text is not overwritten now

A line after an empty line.
^I^IA line with two tabs.

So you can see that tabs were replaced with ^I.

6. Suppress repeated empty output lines using -s option

If it is desired to suppress repeated empty output lines then you can use -s option.

Here is an example of file with repeated empty lines:

$ cat output 
Old Text is overwritten
Old Text is not overwritten now

A line after repeated empty lines.

Now, here is the output when -s option was used :

$ cat -s output 
Old Text is overwritten
Old Text is not overwritten now

A line after repeated empty lines.

So you can see that repeated empty lines got suppressed.

7. Produce concatenated files

Let’s learn how to produce concatenated output files using cat command.

Here is a file output1:

$ cat output1
Old Text is overwritten
Old Text is not overwritten now

Here is a file output2:

$ cat output2
Hi
Hello
Bye

Here is how we can concatenate these two files :

$ cat output1 output2 > output3

$ cat output3
Old Text is overwritten
Old Text is not overwritten now

Hi
Hello
Bye

So we can see that output3 contains concatenated contents of output1 and output2.

Want to read more articles on cat command? Read our article on Researched For You – Best Articles On Cat Command

Leave a Reply

Your email address will not be published. Required fields are marked *