WriteBash - This article is about how to use the
cat command in Linux. It includes an example of using cat command.
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.
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
$ 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.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
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.
linuxmint writebash.com # 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" [DEFAULT] # Ban hosts for one hour: bantime = 600 # Override /etc/fail2ban/jail.d/00-firewalld.conf: banaction = iptables-multiport [sshd] enabled = true EOF
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
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.If you liked this article, then please subscribe to our YouTube Channel for more video tutorials. You can also find us on Twitter and Facebook.
If you appreciate what we share in this blog, you can support us by:
- Stay connected to: Facebook | Twitter | Google Plus | YouTube
- Subscribe email to recieve new posts from us: Sign up now.
- Start your own blog with SSD VPS - Free Let's Encrypt SSL ($2.5/month).
- Become a Supporter - Make a contribution via PayPal.
- Support us by purchasing Ribbon Lite Child theme being using on this website.
We are thankful for your support.