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.
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.
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.β Β 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.
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.
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.
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.
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.
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.
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.
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.
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
- The βheadβ Command in Linux [7 Practical Examples]
- The βtailβ Command in Linux [7 Practical Examples]
- The βlessβ Command in Linux [10 Practical Examples]
- The βmoreβ Command in Linux [10 Practical Examples]
FUNDAMENTALS A Complete Guide for Beginners