Introduction to cat command in Linux/Unix

The cat command is used to display basic text files on the console standard output. With many file names provided as an input, it appends their contents and displays it. For instance, the following output shows how the content of the file named ‘file1’ is displayed by cat with line numbers.



 

For someone familiar with echo command, the cat program is kind of its recursive version applied on all the lines the file(s) provided as input.

Advertisement

Reset Forgotten Ubuntu Linux Password on WSL

 

How to use cat command

The cat command is generally used to view or concatenate parameter files or output files from other programs such as logs, DB Query response files or even bash scripts.

Sometimes, a cat can be used in combination with > and >> operators instead to edit a file from existing ones or from the standard input stream. This is a rare case where cat is used as a text editor. A basic example of this case will be given among others to be provided in following sections

 

1. cat command syntax

As for all native shell commands, the manual with basic syntax on cat is available on your terminal by using man command but we will provide a reminder here. Few examples of execution of this command will also be provided for illustration. Here is the syntax of the command.

$ cat [OPTION] [filename1 [filename2 […]]]

According to the syntax, the command can be executed with or without any OPTION or filename provided as an input. It can also be executed with multiple options and multiple filenames provided as an input.

The list of options available are listed below :

Option

Function

-n

Show all line numbers

-E

Show end of lines as $ character

-b

Show only non-empty line numbers

-s

Display multiple successive empty lines as one

-T

Display Tab characters as ^I

-v

Use ^ and M- notation

-e

Is equivalent to the combined use of –v and –E options

-t

Is equivalent to the combined use of –v and –T options

-A

Is equivalent to the combined use of –v, -E and –T options

-

To designate the standard input as a filename. Ended during execution by the use of ‘Ctrl + D’.

 

For a better understanding, consider the following examples of use of cat

Advertisement

 

2. Create a new file with cat command

You can use following syntax to create a new file and add some data using cat command in Linux or Unix

cat >FILENAME

For example, here I will create a new file cars.txt and add some content to this file. First we will check, we don't have any existing file with the same name:

$ ls -l cars.txt

ls: cannot access 'cars.txt': No such file or directory

Next we will create a new file and add some data into this file (Hit ENTER and press CTRL+D to save and exit):

$ cat >cars.txt

Maruti

Honda

Suzuki

As you can see, a new file cars.txt is created:

$ ls -l cars.txt

-rw-rw-r-- 1 deepak deepak 20 Aug 25 09:05 cars.txt

Check the content of this file:

$ cat cars.txt

Maruti

Honda

Suzuki

 

3. Append data into a new or existing file using cat command

We can also use >> symbol with cat command to either create a new file with some content or also add some new content into an existing file:

Currently I don't have any fruits.txt file:

$ ls -l fruits.txt

ls: cannot access 'fruits.txt': No such file or directory

We will create this file and add some data:

Advertisement

$ cat >>fruits.txt

apple

mango

The file is successfully created and new content is added:

$ cat fruits.txt

apple

mango

Now we will add some more content into the existing fruits.txt file:

$ cat >>fruits.txt

guava

grapes

Verify the content of fruits.txt file:

$ cat fruits.txt

apple

mango

guava

grapes

 

4. Display TAB characters with cat command

In the output of the introduction, lines 4-9 are displayed as empty lines whereas we entered TAB characters in line 6. We view these Tab characters displayed as ^I in the cat output by using the –T option as following.

$ cat -n -T file1

     1  Today is a great day with all my pals

     2  Today is a great day with all my cat

     3  Today is a great day with all my masters

     4

     5

     6  ^I^I

     7

     8

     9

    10  Today is a great day with Jojo

$

 

5. Display End of lines character with cat command

It is possible you don't expect your file to have trailing whitespaces. To check this you can view the file using cat command combined with -E argument. With this option, end of line indicator is displayed with a dollar ($) sign as seen in the example below:

$ cat -E file1

Today is a great day with all my pals$

Today is a great day with all my cat$

Today is a great day with all my masters$

$

$

                $

$

$

$

Today is a great day with Jojo$

$

 

6. Merge repetitive empty lines with cat command

Its often useful to ignore repetitive empty lines to facilitate reading and for a better interpretation of the file. This is done with option –s as following :

$ cat -s -T -n file1

     1  Today is a great day with all my pals

     2  Today is a great day with all my cat

     3  Today is a great day with all my masters

     4

     5  ^I^I

     6

     7  Today is a great day with Jojo

$

On this output, empty line number 4 is displayed once as a merged result of repeated empty lines 4 and 5 of previous output. The same for line number 6 which is displayed for empty lines 7-9 on previous output.

 

7. Show line numbers only on nonempty lines with cat command

On the first output in the introduction section, we displayed all line numbers of ‘file1 with –n option. We are now using the –b to do somethnig similar with the only difference that it doesnt display line numbers on empty lines.

$ cat -b -T file1

     1  Today is a great day with all my pals

     2  Today is a great day with all my cat

     3  Today is a great day with all my masters

 

 

     4  ^I^I

 

 

 

     5  Today is a great day with Jojo

$

Combined with –s option, it still works the same as show on below output :

$ cat -b -s file1

     1  Today is a great day with all my pals

     2  Today is a great day with all my cat

     3  Today is a great day with all my masters

 

     4

 

     5  Today is a great day with Jojo

$

Combined with -n option, it will also still work the same as show on below output. Thus the have similar functions, this case shows us that -b option has priority over -n :

 $ cat -b -n file1

     1  Today is a great day with all my pals

     2  Today is a great day with all my cat

     3  Today is a great day with all my masters

 

 

     4

 

 

 

     5  Today is a great day with Jojo

$

 

8. Concatenate multiple files with cat command

As per its syntax, cat can take multiple filenames as input. With multiple files as input, the result is the appended content of the files displayed on the output.

For the following example, we created a copy of ‘file1’ which doesn't have repetitive empty lines (first command of the output with option -s). You can see the concatenation of the content of ‘file2’ to that of ‘file1’ as from line 11 taking the -n option into consideration to number all lines.

$ cat -s file1>> file2

$ cat -n -E -T file1file2

     1  Today is a great day with all my pals$

     2  Today is a great day with all my cat$

     3  Today is a great day with all my masters$

     4  $

     5  $

     6  ^I^I$

     7  $

     8  $

     9  $

    10  Today is a great day with Jojo$

    11  Today is a great day with all my pals$

    12  Today is a great day with all my cat$

    13  Today is a great day with all my masters$

    14  $

    15  ^I^I$

    16  $

    17  Today is a great day with Jojo$

$

All options can be applied on the concatenated content as if it was that of a unique file with this concatenated content. This means, we could have created a file from ‘file1’ and ‘file2’ to use it later on as unique input to cat command with the same options. The result would have been the same.

 

9. Display text from the standard input with cat command

The  operator (option) is used to refer to the standard input as a filename among cat inputs. In the following example, everything which is written (on the standard input) is immediately displayed by cat (that’s why every line appears twice) until you tap Ctrl+D to indicate  EOF  and resume.

$ cat -

Cat is not a very complex command

Cat is not a very complex command

But it is very useful

But it is very useful

Its just like your little finger

Its just like your little finger

$

 

10. Merge multiple files and add new content using cat command with STDIN

With cat command you can merge multiple files into one file and additionally add extra content during the merge process. The syntax to perform this operation would be:

cat - FILE1 FILE2 FILE3 ... FILEn

Here the position of hyphen (-) can vary and you can add it where you want to add additional data during the merge process. For example, if you intend to add some more data after FILE1 content and before FILE2 then your syntax would be:

cat FILE1 - FILE2 FILE3 ... FILEn

Similarly if you plan to write some text after FILE2 and before FILE3 during the merge process, then your syntax would be:

cat FILE1 FILE2 - FILE3 ... FILEn

 

Let's check this with some examples, here I have two files namely file1 and file2:

cat command examples in Linux/Unix [cheatsheet]

We will merge them into left_merge.txt file and add some data before file1:

$ cat - file1 file2 >> left_merge.txt

Hello World

Press Ctrl+D to exit the prompt once you are done adding your text

Now let's verify the content of left_merge.txt file, as expected "Hello World" is added before file1 into the merged file:

cat command examples in Linux/Unix [cheatsheet]

We can repeat this exercise to add Hello World after file1 and before file2 into centre_merge.txt:

$ cat file1 - file2 >> centre_merge.txt

Hello World

Verify the content of centre_merge.txt file:

cat command examples in Linux/Unix [cheatsheet]

You can perform the same operation with n number of files

 

You can also add multiple STDIN between multiple files, such as

cat FILE1 - FILE2 - FILE3 - FILE4 FILE5 ... FILEn

Here you will be prompted to provide input after FILE1, FILE2 and FILE3.

Let's verify this using an example. Now I have added file3 into our setup with following content:

$ cat file3

Line one in file3

Line two in file3

Line three in file3

Now let us add some content before file1after file1 and after file2 and merge them into multi_merge.txt file:

$ cat - file1 - file2 - file3 >> multi_merge.txt

Adding before file1 (After hitting ENTER I will press Ctrl+D)

I pressed Ctrl+D and now adding some content after file1 (I will again press ctrl+D after hitting ENTER

I pressed Ctrl+D and now adding some content after file2

Now I will press Ctrl+D to exit

HINT:

Such scenarios can become tricky as you will not be informed whether Ctrl+D was properly pressed or not or if you are still writing before file1 or after file1. We just have to carefully remember the sequence and add content if we plan to use this STDIN method

Now you can check the content of multi_merge.txt file:

cat command examples in Linux/Unix [cheatsheet]