Example of using cat command in Linux

Share this post to your friends !

This entry is part 3 of 10 in the series Linux commands: Part 6 - Redirection

WriteBash - 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
example-of-using-cat-command-in-linux-01 Example of using cat command in Linux
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.

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-in-linux-02 Example of using cat command in Linux
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.

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
example-of-using-cat-command-in-linux-03 Example of using cat command in Linux
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
> EOF
example-of-using-cat-command-in-linux-04 Example of using cat command in Linux
Use cat command to create a file with predefined content on terminal.

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.

Continue reading the series«« Previous part: What is pipeline and how to use itNext part: Use sort command to sort pipeline data »»
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.


Share this post to your friends !
If you appreciate what we share in this blog, you can support us by:
  1. Stay connected to: Facebook | Twitter | Google Plus | YouTube
  2. Subscribe email to recieve new posts from us: Sign up now.
  3. Start your own blog with SSD VPS - Free Let's Encrypt SSL ($2.5/month).
  4. Become a Supporter - Make a contribution via PayPal.
  5. Support us by purchasing Ribbon Lite Child theme being using on this website.

We are thankful for your support.

single post bottom banner
«« »»

Got something to say? Join the discussion

Please keep in mind that all comments are subject to our Comment Policy. Your email address will not be published.
This site uses Akismet to reduce spam. Learn how your comment data is processed.