In Linux operating system most of the configuration files, logs and shell scripts are in text file format. That is why we have numbers of text editors in Linux. When you just need to see the content of those files, you can use a simple command named 'cat'. Mainly we use to cat command to view contents, concatenate and redirect the output of files.
From cat manual page it says
cat is a command that concatenate files and print on the standard output
Cat is built-in command in Linux. I believe that all of Linux distribution has this Cat command by default. Let’s start to use how to use it.
1) View the content of file
The easiest way to use cat is just type ‘cat file_name’.
# cat /etc/issue CentOS release 5.10 (Final) Kernel \r on an \m
2) Put the line number on the fly
When reading a configuration file it may that you have a long configuration file. It will be easier to if you can put line numbers on the fly. Use -n parameter to fulfill this purpose.
# 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
3) Number non-blank output lines
Similar with -n parameter, -b parameter will give you numbers on the fly. The difference is -b parameter will only number 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
4) Avoid multiple blank space
-s option squeeze multiple blank lines in the file. ie not more than one blank line
# cat -s welcome Permit time synchronization our time resource but do not permit the source to query or modify the service on this system restrict default kod nomodify notrap nopeer noquery restrict -6 default kod nomodify notrap nopeer noquery Permit all access over the loopback interface. This could be tightened as well, but to do so would effect some of the administration functions restrict 127.0.0.1 restrict -6 ::1 tightened as well, but to do so would effect some of the #
5) Show tabs
You may need to know where are the tabs on your text file. If you do, you can use -T parameter. 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
6) Show the end of lines
-E parameter will 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$
7) Show All
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$
8) View the content per page
When your file can not fit in your screen, you can combine cat with another command to make it displayed per page. Use the pipe ( | ) sign to combine it.
# cat /proc/meminfo | less
# cat /proc/meminfo | more
The difference between less and more is that you can do scroll up and scroll down on less command using PageUp and PageDown buttons. While you can only do scroll down on more command using spacebar.
9) View the contents of 2 files
Let’s say we have 2 text files named linux and desktop located in /root folder. Each of files contains :
File named 'linux' have 'ubuntu, centos, redhat, mint and slackware'
File named 'desktop' have 'gnome kde, xfce, enlightment, and cinnamon'
When you want to view more than 1 file simultaneously, you do like this :
# cat /root/linux /root/desktop ubuntu centos redhat mint slackware gnome kde xfce enlightment cinnamon
10) Sorting file
Again, you can combine cat using another command to make a custom output. 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
11) Redirect standard output
You can also redirect the output to screen or to another file. Just use > sign (greater-than symbol) if you want to redirect the output into a file.
# cat /root/linux > /root/linuxdistro
The above command will create a 'linuxdistro' file that has the same content with /root/linux file. Refer to another example of redirection for multiple files.
# cat F1 welcome1 # cat F2 welcome2 # cat F3 welcome3
# cat F1 F2 F3 > F4 # cat F4 welcome1 welcome2 welcome3 #
12) Creating a new file
There are some ways to create a text file in Linux. One of them is using cat command.
# cat > operating_system Unix Linux Windows MacOS
When you type 'cat > operating system', it will create a file which named operating_system. Then 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.
13) Append (concatenate) the content of file
Below syntax creates a new file named 'newfile.txt', if its doesnt exists and concatenation the contents of two files (file1.txt and file2.txt) to the new file.
Caution Note : if the newfile.txt already exist and have some contents, then will overwrite it
# cat file1.txt file2.txt > newfile.txt
If you want to append the concatenated output of two files into it then use below. Note - If the file 'newfile3.txt' is not present, it would get created
# 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 $
14) Redirect standard input
You can also redirect standard input using < sign (less-than symbol).
# cat < /root/linux
The above command means that /root/linux will be an input for cat command. The output will be like this :
ubuntu centos redhat mint slackware
To make it clearer, let’s see another example :
# cat < /root/linux | sort > linux-sort
That command will be read : sort content from /root/linux file then print the output to linux-sort file
So the output of linux-sort file will be like this :
centos mint redhat slackware ubuntu
That’s some examples of cat command on day-to-day operation. Of course you can explore more detail from cat manual page and experiment with Input-Output redirection operator.
15) 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 :
Article updated on 26/Jan/2015 with few more examples.
16) Display File Content in Reverse
This is really not a cat command example, but it is related. tac is the reverse of cat. As you can imagine, tac will just display the contents of a file in reverse order (lines from bottom is displayed first). For example, the file '/root/linux' is being displayed in reverse as:
# tac /root/linux slackware mint redhat centos ubuntu
17) Printing Files
You can put the standard output from cat command to a printer as its input, type the following command:
# cat linoxide.txt > /dev/lp
Here, /dev/lp is a printing device.
18) 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.
# cat *
Here, the above command will show all the texts inside every files in the current directory.
# cat *.txt
Here, the above command will show all the texts of every files having extension .txt in the current directory.
19) Joining Binary Files
You can concatenate binary files too. In old days most ftp / http downloads were limited to 2GB. Sometime to save bandwidth files size were limited to 100MB. You can combine such files with cat easily by running the below command:
# cat file1.bin file2.bin file3.bin > large.tar.gz
Here, the above command concatenates file1.bin, file2.bin, file3.bin to a single large.tar.gz .
Now, to extract it type the following tar command :
# tar -zxvf large.tar.gz
20) Testing Audio Device
You can send files to sound devices such as /dev/dsp or /dev/audio to make sure sound output and input is working. You can do that by executing the below command:
# cat audio.au >/dev/audio
You can simply use the following command for recording voice sample and play back with it cat command:
# dd bs=8k count=4 </dev/audio >testing123.au # cat testing123.au >/dev/audio
Caution cat command
Even cat looks very simple, we have seen lot of time linux users overwriting very important file using redirection command. 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 ">"
Important : Avoid this # cat > /dev/sda