How to Use CAT Command on Linux System?

This time we continue with the explanation of simple Linux command to learn basics. The command chosen today is the cat command. Cat comes from the word “concatenate“.  In short the main utility is to display the contents of one or more files in the terminal. Here, i will describe most useful examples of CAT command.

Normally the cat command is applied for basically three things in particular:

  • Display the contents of a file
  • Copy text files to another text file
  • Append a text file’s contents to another text file

Being a UNIX command it is ported to all GNU/LINUX distributions and of course, it has options to expand and vary the power of the command itself.  Let’s see the output at the terminal if we do the following command:

cat --help

The command shows us a quick help on the cat command including its options.

The basic usage syntax is:

cat [OPTION] [FILE]…

And the list of commands is shown in this table:

Option Description
-A –show-all Euivalent to –vET
-b –number-nonblank number nonempty output lines, overrides -n
-e equivalent to -vE
-E –show-ends display $ at end of each line
-n –number number all outpout lines
-s –squeeze-blank  suppress repeated empty output lines
-t equivalent to -vT
-T –show-tabs display TAB characters as ^I
-u (ignored)
-V –show-nonprinting   use ^ and M- notation, except for LFD and TAB

Don’t worry; we’ll examine the most useful options with several examples.

Display Content of File

If we want to show the content of a text file, the syntax would be:

cat path_of_the_archive

I’m going to show the content of a file called file1.txt located in the Example folder. Run below command to display file1.txt content.

cat file1.txt

And there we have the content of the file shown on the terminal in above image.

If you would like to display the contents of several files, simply place them next. Run below command to display contents of two files file1.txt and file2.txt.

cat file1.txt file2.txt

The exit at the terminal would be as follows:

You can see the contents of both files one by one in above image.

Identify Specific File Content

As we can see in above image, it simply shows the contents of file 1 and when it ends, it immediately shows the content of file 2 as if they were “concatenated”. If you would like to know where the content of each file ends, then use the -e option to show a $ at the end of the file.

cat –e file1.txt file2.txt

Add Number of Lines in Output

If you want to display the number of lines along with the file content then you can show it by adding -n option. Run below command to display the line numbers while displaying the file content.

cat –n file1.txt file2.txt

Copy Contents of One file to Another

We can also copy the content of one text file to another using CAT command. Run below command to copy content of file1.txt to file4.txt.

cat file1.txt > file4.txt

In this case, the command reads the contents of file1.txt and overwrites the contents of the new file. If it does not exist, it is simply created. Now we can validate above command whether contents have been copied to not. Now we proceed to show the contents of file4.txt to verify that it made the “copy”. Display content of file4.txt by running below command.

cat file4.txt

We can see the content is same as file1.txt in above image.

Combine or Append the Content of One File to Another

The cat command also allows us to combine or append a text file data to another one, thanks to the operator >>

In this case, we will combine the content of file1.txt with that of file2.txt

cat file1.txt >> file2.txt

And then we check the contents of the file2.txt file to validate it.

cat file2.txt

We can see contents have been combined in to this file. Basically the command takes the content of file1.txt and places it at the end of the file2.txt file

As a conclusion we can say that the cat command is a useful and important command to work with text files in GNU/LINUX and everything about the terminal. It is very useful for managing service configuration files on a server.

I hope you like this article. Please follow us on our Facebook page and Twitter handle to get latest updates.

Read More:

Angelo Marquez: I have experience in various Venezuelan governmental and private projects related to Information Technology. Fanatic of free software and Linux in general. Contact me for any query or consulting work using contact us form given on this website.
Related Post
Leave a Comment