The “cat” command in Linux

cat” is derived from “concatenate” in Linux. This command is used to view the content of the file, concatenate files, and create single or multiple files. This is one of the often-used commands in Linux.

let’s have look at the “cat” command:

The syntax for this command:

cat [OPTION]... [FILE]...

only cat command without any option or file name will echo the input i.e. it prints the same content once again

$ cat 
hello world
hello world
lynxbee.com
lynxbee.com

To move out of this echo press “Ctrl+D“.

$ cat
hello world
hello world
lynxbee.com
lynxbee.com

To view or display the content of the text file on the terminal we use filename with an extension next to “cat“.

$ cat CAT.txt
Hey there i am learning Linux and into that currently i am learning CAT command 
$ cat test.txt

Hey look 

Linux is an open source operating system 

and it is easy to learn and operate as well

To view or display the content of the two text files simultaneously on the terminal we use those two filenames with an extension next to the “cat“.

$ cat CAT.txt test.txt
Hey there i am learning Linux and into that currently i am learning CAT command 

Hey look 

Linux is an open source operating system 

and it is easy to learn and operate as well

To create a new text file use “>” next to the “cat” and then after gave the filename with the txt extension.

$ cat > new_test.txt

The above command will give space to write the content which will be written into the newly created text file. To move out of this space for writing press “Ctrl+D“. And check the content with the “cat” command

$ cat > new_test.txt
hey, we created new test file with text format
$ cat new_test.txt
hey, we created new test file with text format

The “>” will overwrite the content of the file i.e. Previously present content will be erased and then new content will be written.

But to append the new content “>>” is used with the cat command. we will verify this by using the “cat” command

$ cat >> new_test.txt
 and now we are appending some new lines          
$ cat new_test.txt
hey, we created new test file with text format
 and now we are appending some new lines

To copy the content from the existing file and write it into the new file we use the file name after the “cat” command after that “>” and lastly new filename with an extension which we want to create and write the content.

$ cat CAT.txt my_self.txt > my_cat.txt
$ cat my_cat.txt
Hey there i am learning linux and into that currently i am learning CAT command 

Hey we, are the engineers bro !!!

To display the content with a number on each non-blank line use the -b option with the “cat” command

$ cat -b test.txt
     1	Hey look 

     2	Linux is an open source operating system 

     3	and it is easy to learn and operate as well

To display the content with a number for each line including the blank line use -n with the “cat” command

$ cat -n test.txt
     1	Hey look 
     2	
     3	Linux is an open source operating system 
     4	
     5	
     6	
     7	
     8	and it is easy to learn and operate as well

To display the content with reduced blank space (multiple blank lines comprise a single line) use -s with the “cat” command. This command will only display the content in this format, it won’t change the actual content of the file.

$ cat -s test.txt
Hey look 

Linux is an open source operating system 

and it is easy to learn and operate as well

This is the working explanation of the “cat” command with some of the options.

Have a look at other basic Linux commands on the website.

Leave a Comment