Basic Cat Command Examples in Linux

By | March 7, 2018

The cat command is one of the most frequently used command in Linux/Unix like operating systems. cat command allows us to create single or multiple files, view contain of file, concatenate files and redirect output in terminal or files. In this article, we are going to find out handy use of cat commands with their examples in Linux.

Display Contents of File

By using below command to show the contents of the file passwd

user@computer:$ cat etc/passwd

View Contents of Multiple Files in terminal

By using below command to show the contents of the multiple files eg.test1 and test2

root@VPS:$ cat test1 test2

Create a File with Cat Command

By using below command to create a file cat3

user@computer:$ cat >test3

Use Cat Command with More & Less Options

By using the command to show the file details with more and less option

user@computer:$ cat test.txt | more
user@computer:$ cat test.txt | less

Display Line Numbers in File

With -n option you could see the line numbers of a file song.txt in the output terminal

user@computer:$ cat -n test.txt

Display Multiple Files at Once

By using below command,Display multiple files at once

user@computer:$ cat test1; cat test2; cat test3

Use Standard Output with Redirection Operator

We can redirect standard output of a file into a new file else existing file with ‘>‘ (greater than) symbol. Careful, existing contents of test1 will be overwritten by contents of test file.

user@computer:$ Cat test > test1

Redirecting Multiple Files Contain in a Single File

Create a file called test4 and all output will be redirected in a newly created file by using below command

user@computer:$ cat test1 test2 test3 > test4

Sorting Contents of Multiple Files in a Single File

This will create a file test5 and output of cat command is piped to sort and result will be redirected in a newly created file.

user@computer:$ cat test1 test2 test3 test4 | sort > test5

This article shows the basic commands that may help you to explore cat command. You may refer man page of cat command if you want to know more options.

Category: VPS

Leave a Reply

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