Suppose you are working on the terminal and you really need to view a text file but you are too lazy to just go to that directory, use a mouse and open it. Well, Linux caters to your need to view the contents of a text file directly into the terminal.

cat stands for ‘concatenate’. Concatenation of something is defined as linking in a series. In this case, we are talking about concatenating or linking the content of the text files. This little command-line utility really proves helpful in your tasks more than you think.

This tutorial is going to be all about the uses of the cat command and some details you need to know about it to make much productive use of this command.

Knowing more about cat

cat helps you in concatenating the text files and this is from where it derives its name ‘cat’. The cat command reads the data from the file and displays its contents on the user’s terminal as the output.

Creation of new files using this command is also possible. Hence, the cat command has got multiple dimensions a user should be aware of.

Let us look at the primitive use of the cat command in this following example.

General Syntax:

cat [options..] [file_name]

Example:

cat demo.txt

Output:

This is a demo file.
This article willl help you with learning the cat command.
cat command is pretty easy  to use.
You can learn about its feqatures in this article.
 End of file


Thank you.

Options available with cat

cat can be used for various purposes in the way we want by using the options provided by Linux. You will learn about some prominent options in this article.

OptionDescription
-nprints line numbers
-somit empty lines in the output
-Tdifferentiate between tabs and spaces
-eshow line ending characters
> operatorallows you to copy content from one file to another
>> (Redirection operator)appends the output to the given file

We will now look at the examples of these options one by one as we move ahead with the article.


Print the line numbers using cat

Using the -n option with the cat command allows you to print the line numbers of the text file.

Example:

cat -n /etc/passwd

Output:

1    1  root:x:0:0:root:/root:/bin/bash
     2	daemon:x:1:1:daemon:/usr/sbin:/usr/sbin/nologin
     3	bin:x:2:2:bin:/bin:/usr/sbin/nologin
     4	sys:x:3:3:sys:/dev:/usr/sbin/nologin
     5	sync:x:4:65534:sync:/bin:/bin/sync
     6	games:x:5:60:games:/usr/games:/usr/sbin/nologin
     7	man:x:6:12:man:/var/cache/man:/usr/sbin/nologin
     8	lp:x:7:7:lp:/var/spool/lpd:/usr/sbin/nologin
     9	mail:x:8:8:mail:/var/mail:/usr/sbin/nologin
    10	news:x:9:9:news:/var/spool/news:/usr/sbin/nologin
    11	uucp:x:10:10:uucp:/var/spool/uucp:/usr/sbin/nologin
    12	proxy:x:13:13:proxy:/bin:/usr/sbin/nologin
    13	www-data:x:33:33:www-data:/var/www:/usr/sbin/nologin
    14	backup:x:34:34:backup:/var/backups:/usr/sbin/nologin
    15	list:x:38:38:Mailing List Manager:/var/list:/usr/sbin/nologin

Here, every line is designated with a number. This also helps in getting an idea about the total number of lines in the file. The empty lines in the text file are also assigned the numbers when -n option is used.


Using the > operator to copy file content

The > operator can be used with the cat command to copy the contents of the file into some other file. We will understand this better by example.

General Syntax:

cat file1 > file2

Here, the contents of the file1 will be copied into the file2. It is not necessary that file2 should already exist. If it exists then well and good but if it doesn’t, then this command will create it for you.

Example:

cat demo.txt > test.txt

Output:

gaurav@ubuntu:~$ cat test.txt
This is a demo file.
This article willl help you with learning the cat command.
cat command is pretty easy  to use.
You can learn about its feqatures in this article.
 End of file


Thank you.


gaurav@ubuntu:~$

Here, the contents of the file ‘demo.txt’ are directed to or copied to the file ‘test.txt’. Now, in this case the file test.txt did not exist before firing this command. It was in fact created by this command.


Using the >> operator to append file content

We can use the >> (redirecting operator) with the cat command to append the contents of the file.

In appending the files, the output of one command is send as the input to a file or some other command.

For example, if I run the command cat /etc/ group , then the information of all the groups present on your Linux system will be displayed on the terminal. Now suppose, you would like to get these details in the form of a file, then, in this case, you can use the >> redirection operator along with the cat command.

General Syntax:

cat /dir1/file.txt >> [new_file]

Example:

cat /etc/group >> groups.txt

This command will send the output of the cat /etc/group command, as an input to the file group.txt.

Output:

gaurav@ubuntu:~$ cat group.txt
root:x:0:
daemon:x:1:
bin:x:2:
sys:x:3:
adm:x:4:syslog,gaurav
tty:x:5:
disk:x:6:
lp:x:7:
mail:x:8:
news:x:9:
uucp:x:10:

Omitting empty lines in the output

In the text files, there may be some empty lines which would be increasing the length of the output. The repeated empty lines can be omitted using the-s option with the cat command.

Let us look at one sample text file.

This is a demo file.

This article willl help you with learning the cat command.
cat command is pretty easy  to use.
You can learn about its feqatures in this article.



Above two lines are empty.
 End of file


Thank you.

You can see in the highlighted part that there are 3 empty lines. Now, let us use the -s option to suppress the extra empty lines.

Example:

cat -s demo.txt

Output:

This is a demo file.

This article willl help you with learning the cat command.
cat command is pretty easy  to use.
You can learn about its feqatures in this article.

Above two lines are empty.
 End of file

Thank you.

You can see that the extra empty lines are now omitted from the output. This option is useful when you are dealing with large outputs onto your terminal.


Indicating end of lines in the file

When the -e option is used with the cat command, this displays the invisible symbol which represents the end of every single line. This end of any line is given by the ‘$‘ symbol.

General Syntax:

cat -e [filename]

Example:

cat -e /etc/issue

Output:

Ubuntu 18.04.5 LTS \n \l$
$

Here, the output shows that the ending of every line is marked with the ‘$‘ symbol.


Create a new file with cat

cat command can also be used to create a new file like any other text editor like nano or vim. You can edit this newly created file using the terminal.

General Syntax:

cat > [newfile]

Example:

cat > report.txt

Output:

gaurav@ubuntu:~$ cat > report.txt
This is a report file required for maintaining the logs.

Kindly do not modify this file.

End of file
...
..
^C
gaurav@ubuntu:~$

Thus, the cat command has created a new file name report.txt.


Display the content of all text files in a folder

This is one of the interesting activities you can perform using the cat command. If you want to display the content of more than one text files at a time, then you can use the cat command in the following way.

General Syntax:

cat *.txt

This command will display the content of all the text files in the directory you are current placed into.

Let us first look at two demo files sample1.txt and sample2.txt.

gaurav@ubuntu:~/cat$ cat sample1.txt
This is the output of the first file 'sample 1'.
Thank You.
gaurav@ubuntu:~/cat$

gaurav@ubuntu:~/cat$ cat sample1.txt
This is the output of the first file 'sample 1'.
Thank You.
gaurav@ubuntu:~/cat$

Example:

cat *.txt

This command will display the content of both the text files present in my current working directory as a single output.

Output:

gaurav@ubuntu:~/cat$ cat *.txt
This is the output of the first file 'sample 1'.
Thank You.
This is the output of the second file 'sample2'.
Thank You.
gaurav@ubuntu:~/cat$

Conclusion

In this tutorial, we learned about all the basic functions of the cat command in Linux. You can now use it for concatenating different text files as well as modifying the content inside a text file right from your terminal. You can also use it as text editor as it helps you to create new files as well. The versatile nature of this cat command makes it pretty popular among Linux users.