15 Practical examples of cat command

Update on:
May 14, 2020

Cat command, a short form of “concatenate” is one of the most important and frequently used on Linux/Unix like operating systems. It is use to create single or multiple files, view the content of the file, concatenates files and redirect output in any file or on the terminal. In this article, we see the uses of cat command with their examples in Linux.

cat command Syntax:
cat [OPTION] [FILE]...

Suggested Reading: Linux sed command Structure


Display Contents of File

To display the content of the file, you can use the cat command with the filename.

In the below example, It will show the content of /etc/password file.

# cat /etc/passwd



View Contents of Multiple Files in terminal

To display the content of multiple files, you can use the cat command with multiple filenames.

In below example, it will display contents of test01 and test02 file in terminal.

# cat test01 test02


Hello everybody
Hi world

Create a File with Cat Command

You can create a file called test02 file with below command

# cat > test02

The command waits for input from the user, type desired text and press CTRL+D. It will be writing text into the file test02.

You can see the content of the file with the following cat command.

# cat test02
hello everyone, how do you do?

Display Line Numbers in File

You can use the “-n” option with cat command to see the content of the file with lines number.

Using the below command, you can see the content of test01.txt file with line numbers.

# cat -n test01.txt
1 Hello Everyone
2 I am Satish Kumar
3 A Computer Engineer
4 Founder of LinuxConcept
5 Here to help you
6 Sharing Linux Pratical Knowladge
7 With Everyone.

Use Cat Command with More & Less Options

If file having the large number of content that won’t fit in output terminal and screen scrolls up very fast, we can use parameters more and less with cat command as shown below example.

# cat movies-list.txt | more
# cat movies-list.txt | less

Copy the contents of one file to another file

The cat command can use to copy the content of one file to another file.

In the below example, it copies the content of file text01 to file text02.

# cat text01 > text02

The output redirection operator (>) use to flush the destination file before moving new content. If you want to move new content with existing content in the destination file, use another redirection operator (>>) which is use to appends the contents into the destination file.

Use cat command to highlight line-ends

You can use the “-e” option with cat command to display “$” at the end of the line and in the space between the paragraphs—this option we use to squeeze multiple lines in a single line.

Check below example for better understanding:

# cat -e test
hello everyone, how do you do?$
I am fine.$
Hows your learning going?$

Display Lines in File which are Tab separated

To display TAB separated lines in a file, you can use the cat command with the “-T” option.

In the below example, you can see the TAB space filled with ‘^I’ character.

hello ^Ieveryone, how do you do?
I ^Iam fine.
^I^IHow’s your learning ^Igoing?
Let’s do ^Isome practice in Linux Server.

Display Multiple File’s contents at Once

You can use the semicolon (;) separator with several cat command as shown in below example here we display the content of three files named as following text01, text02, and text03.

# cat text01; cat text02; cat text03
This is text01 file
This is text02 file.
This is text03 file.

Use Input with Redirection Operator with cat Command

The cat command can use with the standard redirect operator (<) to get the input for the command.

In the below example, cat command gets the input from file text01 and the output shown in the terminal.

# cat < text01
This is text01 file.

Concatenate multiple file contents in a single file

You can use the cat command to redirect content of multiple files into a single file.

In the below example, it redirects content of files text01, text02, and text03 into the file text04.

# cat text01 text02 text03 > text04

Sorting Multiple File’s content and redirect output in a Single File

In the below example, it creates a file text04 and output of the cat command piped to sort and result get redirect to file text04.

# cat text01 text02 text03 | sort > test4

Use cat to display non printing characters

You can use the cat command to display non-printable characters in a file by using the “-v” option.

In below example, I am showing non-printable characters of file test123.txt.

# cat -v test123.txt

The “-A” option of cat command

The “-A” option is doing a job as the combination of the “-v”, “-E” and “-T” option. If you have some specific requirements where need to use these three options then you can use only the “-A” option which do the job of all three options, as shown in below example:

# cat -A test.txt

Related Posts

What is the Linux command Line?

Introduction The Linux command line, also known as the terminal or shell, is a powerful tool for interacting with your computer and managing your files and programs. It allows you to perform various tasks, such as creating and editing files, running programs, and...

The 30 most useful Linux commands

This article will introduce some practical and common Linux or Unix command , which is the Linux system administrators normally use the command . This article is not a complete list, but a brief list of commands that can be useful...

15 Practical examples of tcpdump command

15 Practical examples of tcpdump command

The tcpdump is the most essential and powerful command-line network packet sniffing tool. We use it to analyze network packets, capture or filter TCP/IP packets that transferred and received over the network on a specific network interface. It is available in almost...

15 Useful “df” Commands to Check Disk Space in Linux

15 Useful “df” Commands to Check Disk Space in Linux

There are several Linux tools available on the internet to check disk space utilization; we can use any of them. But, Linux has a best built-in command-line utility called “df”. The “df” is the short form of “disk filesystem”, we can get detailed information on used...

Follow Us

Our Communities

More on Linux Command

The Ultimate Managed Hosting Platform
Load WordPress Sites in as fast as 37ms!



Submit a Comment

Your email address will not be published. Required fields are marked *

16 + 10 =