Table of Contents
This article is about how to use the cat command in Linux. It includes an example of using cat command.
The cat command is one of the most common commands in Linux, the administrator uses the cat command quite often. So what does cat command use to do? And how to use it?
Cat command syntax
First, what is the cat command for? It reads one or more files and can copy them to standard output.
The cat command has a simple structure as below.
$ cat [file...]Example of using cat command
Read the file content
This is the most commonly used purpose of it, we often use it to read the content contained in a file. The usual cat command reads files with relatively short content (about one screen length). If you want to read a long file, use the less command.
For example, we will read the contents of a file as writebash.txt:
$ cat writebash.txt
Used to join files
Because of the ability to copy files, the cat command has the ability to use to join files into a single file.
This is something you get a lot of when downloading a large file, for example, an OS file, movie or PDF file divided into sections.
Recommended Reading: How to use pipeline in Linux?
A common example is when you download a big movie. Parts are usually have names as movie.mp4.001, movie.mp4.002, movie.mp4.003 … movie.mp4.099.
Now we just need to run the command as shown below using wildcard, it will automatically join the files in the file numbering order.
$ cat movie.mp4.00* > movie.mp4Let’s look at another example, we have 2 text files and now we will join these 2 files into 1 new file.
$ cat file1.txt file2.txt > newfile.txt
Create file with content
The cat command can copy standard input to standard ouput, thanks to this special feature, it is often used to create files with predefined content.
Recommended Reading: How to write a bash script?
Now try typing the command below.
$ cat > file3.txtThen, type the content into the cursor on the terminal window and press Ctrl + D (it’s the end of file - EOF) to finish.
$ cat > file3.txt
This is demo content typed by Danie Pham - WriteBash.comNow go to the actual bash script example. In scripts, we use it to create config files or other script files, or any file.
Below is the demo content in a script file.
$ cat > /etc/fail2ban/jail.local <<"EOF"
[DEFAULT]
# Ban hosts for one hour:
bantime = 600
# Override /etc/fail2ban/jail.d/00-firewalld.conf:
banaction = iptables-multiport
[sshd]
enabled = true
EOF
Note: EOF put inside double quote. It helps variables in the content section not be executed.
Or if you type in the terminal window, the command will be similar to the image below.
$ cat > file4.txt <<"EOF"
> demo line 1
> demo line 2
> demo line 3
> EOF
Conclusion
After finishing this article, you can learn how to redirect input/output data with the cat command. You can also see its application in the process of writing bash script files. It is really a powerful command in Linux.
(This is an article from my old blog that has been inactive for a long time, I don’t want to throw it away so I will keep it and hope it helps someone).