Cat command is used in Linux to concatenate files and print on the standard output. Cat is commonly used to view the content of a file, for redirection, create, and concatenate files. Cat is a built-in command in all Linux distribution by default.
The syntax of cat command is:
cat [OPTION] [FILE]
1) View the content of a file
To view the content of a file, use cat command followed by the filename.
$ cat /etc/issue CentOS release 5.10 (Final) Kernel \r on an \m
To open and view multiple file contents, use cat command followed by multiple file paths:
$ cat /etc/issue /etc/lsb-release Ubuntu 16.04.4 LTS \n \l DISTRIB_ID=Ubuntu DISTRIB_RELEASE=16.04 DISTRIB_CODENAME=xenial DISTRIB_DESCRIPTION="Ubuntu 16.04.4 LTS" root@testwebsrvlinode02:~# cat /etc/issue Ubuntu 16.04.4 LTS \n \l
2) Copy to another File
To copy content to another file, use operator > with cat command. Let me show you example.
The following command will create a 'linuxdistro' file that has the same content with /root/linux file. If the file already exists it will rewrite the contents.
$ cat /root/linux > /root/linuxdistro
To copy content from multiple files, use the following example:
$ cat file1.txt file2.txt > newfile.txt
3) Append content to the end of file
Instead of overwriting if you like to append content to the end of the file use operator >>.
$ cat file1.txt file2.txt >> newfile3.txt
Let’s see an example;
$ cat file1.txt one two $ cat file2.txt three $ cat file1.txt file2.txt > newfile.txt $ cat newfile.txt one two three $
4) Creating a new file
There are many ways to create a text file in Linux. To create a file, use cat command followed by redirection operator and file name to create. On press Enter and add the file content, once done press CRTL + D to save the file.
When you type 'cat > operating_system', it will create a file which named operating_system. Then press Enter you will see a blank line below the cat command. You can type the content that you want. We typed Unix, Linux, Windows and MacOS for example. When you’re done, press 'Ctrl +D' to save the content and exit from cat command. You will see a file named 'operating_system' is created in the current folder with the content that you add before.
# cat > operating_system Unix Linux Windows MacOS
Note: If 'operating_system' file already exists, then it will overwrite it.
5) Redirect standard input
You can also redirect standard input using operator < (less-than symbol).
In the following example, the input for cat command would the content of the /root/linux.
$ cat < /root/linux
ubuntu centos redhat mint slackware
The following command will take the content of the file /root/linux and sort the output to a file:
$ cat < /root/linux | sort > linux-sort centos mint redhat slackware ubuntu
6) Combine cat command with pipe
It's very useful to combine cat command with pipe operator. Let's take a few of examples.
When your file cannot fit on your screen, you can combine cat with more or less command using | to make it displayed per page.
# cat /proc/meminfo | less
# cat /proc/meminfo | more
to remove specific line number using cat command use with sed command, as:
The following command removes line number 3 from the file named sample.txt:
$ cat -n /home/sample.txt | sed '3d' 1 text 1 2 text 2 4 text 4 5 text 5 6 text 6
If you want to sort it, you can combine it with sort command. Here’s the example:
$ cat /root/linux | sort centos mint redhat slackware Ubuntu
7) Add line numbers to file
To add line numbers to a file, use cat command with -n option followed by the filename. For example:
# cat -n /etc/ntp.conf 1 # Permit time synchronization our time resource but do not 2 # permit the source to query or modify the service on this system 3 restrict default kod nomodify notrap nopeer noquery 4 restrict -6 default kod nomodify notrap nopeer noquery 5 6 # Permit all access over the loopback interface. This could be 7 # tightened as well, but to do so would effect some of the 8 # administration functions 9 restrict 127.0.0.1 10 restrict -6 ::1
8) Number non-blank output lines
Similar with -n option, -b option will show line numbers in the file. The difference is that -b option will only number the non-blank lines.
$ cat -b /etc/ntp.conf 1 # Permit time synchronization our time resource but do not 2 # permit the source to query or modify the service on this system 3 restrict default kod nomodify notrap nopeer noquery 4 restrict -6 default kod nomodify notrap nopeer noquery 5 # Permit all access over the loopback interface. This could be 6 # tightened as well, but to do so would effect some of the 7 # administration functions 8 restrict 127.0.0.1 9 restrict -6 ::1
9) Remove repeated blank lines
To suppress repeated empty output lines use -s option. This will squeeze multiple blank lines in the file. ie not more than one blank line will show up.
$ cat /home/sample.txt text 1 text 2 text 4 text 5 $ cat -s /home/sample.txt text 1 text 2 text 4 text 5
10) Show tabs in file
To indicate tabs in a file, use option -T. Tab will be represented by ^I characters.
# cat -T /etc/hosts # Do not remove the following line, or various programs # that require network functionality will fail. 127.0.0.1 ^I^I localhost.localdomain localhost ::1 ^I^I localhost6.localdomain6 localhost6
11) Show end of lines
Use -E parameter to display '$' at end of each line. Here’s the example :
# cat -E /etc/hosts # Do not remove the following line, or various programs$ # that require network functionality will fail.$ 127.0.0.1 localhost.localdomain localhost$ ::1 localhost6.localdomain6 localhost6$
If you want to combine between -T and -E, you can use -A parameter.
# cat -A /etc/hosts # Do not remove the following line, or various programs$ # that require network functionality will fail.$ 127.0.0.1^I^Ilocalhost.localdomain localhost$ ::1^I^Ilocalhost6.localdomain6 localhost6$
12) Read Content until a Specific Pattern
When you are reading from stdin, you can read until a line that contains a specific pattern. In the example below, the block of lines are read from stdin (until EOF) and printed on the standard output.
# cat <<EOF > Redhat > Ubuntu > EOF
And the Output of the above will be :
13) Display all files with wildcard
Combine cat command with Linux wildcard character to view the texts of the files at once in the current directory.
The following command will show all the texts inside every files in the current directory.
# cat *
To show all the texts of every file having extension .txt in the current directory.
# cat *.txt
Even cat command looks very simple, we have seen a lot of time Linux users overwriting very important files using redirection operator. Make sure the name of the file you are creating does not match the name of an existing file. If you plan to append make sure you use ">>" and not ">".
In this article, we learned about Linux cat command and its option with examples. Please provide your feedback in the below comment section.