The “cat” Command in Linux [10 Practical Examples]

The cat command is very useful for viewing the contents of single and multiple files on the Ubuntu terminal. In this article, I will describe all the features and uses of the cat command in Linux with multiple examples.

A. Description

The cat command prints the contents of the file specified. Generally, cat (concatenates) reads the contents of the files fed to its arguments and prints them serially on the terminal.

B. Syntax

The syntax for the cat command is pretty simple which is the command itself followed by some specific options.

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

Note: In the above syntax OPTION enclosed by a square bracket and followed by 3 dots represents that multiple options can be utilized at the same time.

C. Options

Many useful options are available for the cat command. I have listed some of the most used ones here. However, if you want to know more about options for the cat command you can always check the man page for the cat command.

man cat

Useful Options

  • -E, –show-ends — Display $ at end of each line.
  • -n, –number — Displays line numbers when utilized.
  • -s, –squeeze-blank — suppress repeated empty output lines.
  • -T, –show-tabs— display TAB characters as ^I.

NB: The options in Linux CLI (Command Line Interface) are all case-sensitive, So be cautious while using them.

Practical Examples of the “cat” Command in Linux

The cat command in Linux is an easy-to-use command which is used to concatenate(print) the contents of a text file. Some of the most useful applications of the cat command have been illustrated below. In this article, I have worked with the following files, you can also download those files if you want to work with the same files.

Example 1: Display the Contents of a Single File

To display any file on your Ubuntu Terminal you have to follow the below procedure. Here, I have chosen my file1.txt file to show on the terminal.

Steps to Follow >

➊  At first open the Ubuntu Terminal.

  Type the following command in the command prompt:

cat file1.txt

➌ Now, press the ENTER button.

Output >

You can see in the picture that the contents of my file are displayed on the terminal.Display the contents of a single file

Example 2: Display the Contents of Multiple Files

To display the contents of multiple files serially on your Ubuntu Terminal you have to follow the below procedure. Here, I have chosen my file1.txt,  file2.txt, and file3.txt files to show on the terminal.

Steps to Follow >

➊  At first open the Ubuntu Terminal.

  Type the following command in the command prompt:

cat file1.txt  file2.txt  file3.txt

➌ Now, press the ENTER button.

Output >

You can see in the picture that the contents of all 3 files are displayed one by one on the terminal.Display the contents of multiple files using cat command

Example 3: Create a New File Using the “cat” Command in Linux

You can create a new file on your desktop using cat. To create a new file you should follow the below procedure. Here, I have created a new file4.txt file.

Steps to Follow >

➊  At first open the Ubuntu Terminal.

  Type the following command in the command prompt:

cat >file4.txt

➌ Now, press the ENTER button.

Your command prompt will be waiting for you to put some lines or values there.

➍ Type the following lines in the command prompt:

This is my first file

It was the first line of the file

➎ Press CTRL+D and then press the ENTER button.Create a new file using "cat" command in Linux Type the following command in the command prompt to see the contents of the newly created file:

cat file4.txt

➐ Finally, press the ENTER button again.

Output >

You can see in the picture that the lines are added to the file4.txt file on my desktop.printing the newly created file

Example 4: Using the “cat” Command to Redirect the Contents of a Single File

You can redirect the contents of a file to another by using the cat command. Here, I have redirected the content of my file1.txt file to the file2.txt file. To do this you need to follow the below procedure:

Steps to Follow >

➊  At first open the Ubuntu Terminal.

  Type the following command in the command prompt:

cat file1.txt > file2.txt

➌ Now, press the ENTER button.

➍  Type the following command in the command prompt to see the contents of the file:

cat file2.txt

➎ Finally, press the ENTER button again.

Output >

You can see in the picture that the contents of the file1.txt file have overwritten the contents of the file4.txt file on my desktop.Redirecting the contents of a single file

Example 5: Redirect the Contents of Multiple Files

You can redirect the contents of multiple files to another file by using the cat command. Here, I have redirected the contents of my file1.txt and file2.txt files to the file3.txt file. To do this you need to follow the below procedure:

Steps to Follow >

➊  At first open the Ubuntu Terminal.

  Type the following command in the command prompt:

cat file1.txt file2.txt > file3.txt

➌ Now, press the ENTER button.

➍  Type the following command in the command prompt to see the contents of the file:

cat file3.txt

➎ Finally, press the ENTER button again.

Output >

You can see in the picture that the contents of the file1.txt and file2.txt files have overwritten the contents of the file3.txt file on my desktop.Redirecting the contents of multiple files

Example 6: Display the Contents of a File in Reverse Order

To display any file in vertical reverse order on your Ubuntu Terminal by using tac. You have to follow the below procedure to execute it. Here, I have chosen my file1.txt file to show on the terminal.

Steps to Follow >

➊  At first open the Ubuntu Terminal.

  Type the following command in the command prompt:

cat file1.txt

➌ Now, press the ENTER button.

It will show you the contents of the file.

➍ Type the following command in the command prompt:

tac file1.txt

➎ Finally, press the ENTER button again.

Output >

You can see in the picture that the contents of my file are displayed on the terminal in vertical reverse order.Display contents of a file in reverse order

Example 7: Append Text to Existing File

You can add some texts to any file by using the cat command. Here, I have added a line to my file1.txt file. To do this you need to follow the below procedure:

Steps to Follow >

➊  At first open the Ubuntu Terminal.

  Type the following command in the command prompt:

cat >> file1.txt

➌ Now, press the ENTER button.

➍  Type the following line in the command prompt to add the line with the contents of the file:

This is the actual first file

➎ Press CTRL+D and then press the ENTER button.

➏ Type the following command in the command prompt:

cat file1.txt

➐ Finally, press the ENTER button again.

Output >

You can see in the picture that the line is added at the end of the contents of the file1.txt file on my desktop.Append text to existing file using cat command in Linux

Example 8: Append Multiple Files’ Contents to Another File Using the “cat” Command

You can append the contents of multiple files to another file by using the cat command. Here, I have appended the contents of my file1.txt and file2.txt files to the file3.txt file. To do this you need to follow the below procedure:

Steps to Follow >

➊  At first open the Ubuntu Terminal.

  Type the following command in the command prompt:

cat file1.txt file2.txt  >>  file3.txt

➌ Now, press the ENTER button.

➍  Type the following command in the command prompt to see the contents of the file:

cat file3.txt

➎ Finally, press the ENTER button again.

Output >

You can see in the picture that the lines of the file1.txt and file2.txt files are added at the end of the lines of the file3.txt file on my desktop.Append the contents of multiple files into one file

Example 9: Using the “cat” to Combine Multiple Files into Another File

You can combine multiple files’ contents and redirect them to another newly created file. Here, I have combined my file1.txt, file2.txt, and file3.txt files and put them in a newly created file named file4.txt.  You have to follow the below procedure to execute it.

Steps to Follow >

➊  At first open the Ubuntu Terminal.

  Type the following command in the command prompt:

cat file1.txt file2.txt file3.txt  >  file4.txt

➌ Now, press the ENTER button.

➍  Type the following command in the command prompt to see the contents of the file:

cat file4.txt

➎ Finally, press the ENTER button again.

Output >

You can see in the picture that a new file has been created named file4.txt. The contents of the file1.txt, file2.txt, and file2.txt files are added to the file4.txt file on my desktop.Combine the contents of multiple files into a new file

Example 10: Manage Large Files Using the “cat” Command

Sometimes concatenating large files can be very hard to see through the terminal. I have managed a large text file named paragraph. If you want to manage the exact same file you can download it from here. To manage large files using the cat command you should follow the below procedure.

Steps to Follow >

➊  At first open the Ubuntu Terminal.

  Type the following command in the command prompt:

cat paragraph | less

➌ Now, press the ENTER button.

Output >

You can see in the picture that the contents of my file are displayed on the terminal page by page in a list format which is easier to read. Press the SPACE button to go to the next page of the file. You can also press the ENTER button to scroll to the next page line by line.Opening the first page of a large file using cat command in Linux

Opening one page at a time to manage a large file

Conclusion

After completing this article, you will acquire all the knowledge about the cat command in Linux. With the given practical examples, you will be able to execute a lot of commands to become a power user.


Similar Readings

5/5 - (1 vote)
LINUX
FUNDAMENTALS
A Complete Guide for Beginners Enroll Course Now
icon linux
Borhan Uddin

Hello, I am Borhan Uddin, a resident of Dhaka, Bangladesh. I have completed my undergraduate degree in Electrical and Electronic Engineering (EEE) from Chittagong University of Engineering and Technology (CUET). I love to spend my leisure by playing and watching various movies & dramas. Currently, I am working as a Linux Content Developer Executive here. I would like to learn more about Linux every day of the week and would be keen to share it with you rapidly. Read Full Bio

Leave a Comment