In this tutorial, we are going to explain some basic cat commands in Linux, that are applicable on various distributions such as Ubuntu, Debian, CentOS, AlmaLinux and etc.
The “cat” command is a shortcut of the word “concatenate” and is a very useful command that is frequently used, by system administrators and DevOps engineers. With this command you can easily view files, create them, filter information from them, display line numbers in files and etc.
In this post, the cat command will be explained with real examples on Ubuntu 20.04. You can use the Linux distribution of your choice. Let’s get started!
Table of Contents
Prerequisites
- Fresh install of Linux distribution
- User privileges: root or non-root user with sudo privileges
Update the System
It is recommended to update the system packages to the latest versions available after a fresh installation of the server.
Once, the system is updated we are ready to show you the basic cat commands with examples.
1. Create a File
To create a file with “cat” execute the following command in some directory on your server:
Enter some text and press CTRL+D to save the content, of the newly created file.
To check if the file is created successfully list the content of that directory:
2. Check File Content
To check the content of a file, you can execute the following command:
You will receive the output on the command line directly:
3. Filter Specific Content
Sometimes, we need some specific lines of the file to be visible as output and this can be done with the cat and grep commands combined.
You should receive the following output:
4. Print Line of Numbers
To print the line of numbers in the file execute the following command:
You will receive output with numbers before every new line:
5. Display Content of Multiple Files
We will create three files, test1.txt, test2.txt, and test3.txt with the content “Test file 1”, “Test file 2”, and “Test file 3” respectively. Once created execute the following command to display the content of all three files at once:
You will receive the following output:
6. Reverse Displaying of the Content
First, we will create a file with the following content and order:
To display the content of the file in reverse order, execute the following command:
You should receive the following output:
7. Append Text to File
To append a new text to the existing file execute the cat >> order.txt command, enter the text, and press CTRL + D to save the changes.
Now, check the content of the file with appended text.
You should receive the following output:
8. Display the head of the file
To display the head of the file, use the following command:
Where number defines how many lines should be displayed from the beginning of the file.
You should receive the following output:
9. Display the tail of the file
To display the tail of the file, use the following command:
Where number defines how many lines should be displayed from the bottom(last lines) of the file.
You should receive the following output:
To understand better the content of the file with dummy text:
10. The “cat” Documentation
In this last heading, we are going to show you how to display all options of the cat command for future needs. Execute the command below:
You will receive a huge list of definitions and options with examples.
That’s it. You were able to learn the basic cat commands on your Linux OS, with real examples and usage.
If you need to filter or display some content of files, and you do not know how to do that, you just need to sign up for one of our Linux VPS plans, submit a support ticket and our admins will do the rest. We are available 24/7.
P.S. If you liked this post about the basic cat commands on Linux with examples, please share it with your friends on the social networks or simply leave a reply below. Thanks.