Example of using cat command in Linux

by Daniel Pham
Published: Updated:
This entry is part 3 of 10 in the series Linux commands: Part 6 - Redirection

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
use cat command to view content of a file
Use cat command to view content of a file.

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.003movie.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.mp4

Let’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
Example of using cat command: use cat command to join multi files into single file
Example of using cat command: use cat command to join multi files into single file.

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.txt

Then, 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.com

Now 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"
# Ban hosts for one hour:
bantime = 600

# Override /etc/fail2ban/jail.d/00-firewalld.conf:
banaction = iptables-multiport

enabled = true
use cat command to create a file with predefine content
Use cat command to create a file with predefined content.

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
use cat command to create a file with predefine content on terminal
Use cat command to create a file with predefined content on terminal.


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).

0 0 votes
Article Rating
Series Navigation«« Previous part: What is pipeline and how to use itNext part: Use sort command to sort pipeline data »»

You may also like

Notify of
Inline Feedbacks
View all comments

DevOps Lite is a personal blog specializing in technology with main topics about DevOps, DevSecOps, SRE and System Administrator. Articles are shared for free and contributed to the community.



Subscribe my Newsletter for new blog posts. Stay updated from your inbox!

© 2021-2024 DevOpsLite.com – All rights reserved.

Please write sources “DevOpsLite.com” when using articles from this website.

This website uses cookies to improve your experience. We'll assume you're ok with this, but you can opt-out if you wish. Accept Read More

Would love your thoughts, please comment.x

Adblock Detected

Please support us by disabling your AdBlocker extension from your browsers for our website.