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.
sudo apt update -y && sudo apt upgrade -y
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:
cd /opt cat > testfile.txt
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:
ls -al /opt
2. Check File Content
To check the content of a file, you can execute the following command:
cat /etc/apache2/sites-enabled/mydomain.conf
You will receive the output on the command line directly:
<VirtualHost *:80> ServerName mydomain.com ServerAlias www.mydomain.com DocumentRoot /var/www/html/ ErrorLog ${APACHE_LOG_DIR}/mydomain.com_error.log CustomLog ${APACHE_LOG_DIR}/mydomain.com_access.log combined </VirtualHost>
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.
cat /etc/apache2/sites-enabled/mydomain.conf | grep DocumentRoot
You should receive the following output:
DocumentRoot /var/www/html/
4. Print Line of Numbers
To print the line of numbers in the file execute the following command:
cat -n testfile.txt
You will receive output with numbers before every new line:
root@vps:/opt# cat -n testfile.txt 1 Lorem Ipsum is simply dummy text of the printing and typesetting industry. 2 Lorem Ipsum has been the industry's standard dummy text ever since the 1500s. 3 It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. 4 It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages.
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:
cat test1.txt test2.txt test3.txt
You will receive the following output:
root@vps:/opt# cat test1.txt test2.txt test3.txt Test file 1 Test file 2 Test file 3
6. Reverse Displaying of the Content
First, we will create a file with the following content and order:
First line. Second line. Third line.
To display the content of the file in reverse order, execute the following command:
tac order.txt
You should receive the following output:
root@vps:/opt# tac order.txt Third line. Second line. First line.
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.
cat >> order.txt This is a new line. This is the fourth line in the file.
Now, check the content of the file with appended text.
cat order.txt
You should receive the following output:
root@vps:/opt# cat order.txt First line. Second line. Third line. This is a new line. This is the fourth line in the file.
8. Display the head of the file
To display the head of the file, use the following command:
cat testfile.txt | head -number
Where number defines how many lines should be displayed from the beginning of the file.
cat testfile.txt | head -2
You should receive the following output:
root@vps:# cat testfile.txt | head -2 Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s.
9. Display the tail of the file
To display the tail of the file, use the following command:
cat testfile.txt | tail -number
Where number defines how many lines should be displayed from the bottom(last lines) of the file.
cat testfile.txt | tail -2
You should receive the following output:
root@vps:# cat testfile.txt | tail -2 It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages.
To understand better the content of the file with dummy text:
root@vps:# cat testfile.txt Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages.
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:
man cat
You will receive a huge list of definitions and options with examples.
root@vps:~# man cat NAME cat - concatenate files and print on the standard output SYNOPSIS cat [OPTION]... [FILE]... DESCRIPTION Concatenate FILE(s) to standard output. With no FILE, or when FILE is -, read standard input. -A, --show-all equivalent 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 output lines -s, --squeeze-blank suppress repeated empty output lines -t equivalent to -vT -T, --show-tabs display TAB characters as ^I
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.