16 Cat Command Examples in Linux

cat stands for Concatenate. cat command is one of the basic command in Linux & Unix, it is used to create new files, concatenate files and and also used to view the contents of files on the standard output. In this article, we will discuss cat command with 16 practical examples.

Basic syntax of cat command :

# cat <options> <File>

Options of cat command,

cat-command-options

Example 1)  Create a new file with cat  command

Let’s suppose i want to create a new file with name ‘linux_world’. Type the following cat command followed by the text you want in to insert in the file. Make sure you type ‘Ctrl-d’ at the end to save the file.

[[email protected] ~]# cat > linux_world
Hi this is my first file in linux.
Linux always rocks
Thanks
[[email protected] ~]#

Example 2)  View the Contents of existing file

To display or view the contents of existing file using cat command, use the below syntax

# cat {file_name}

To view the contents of linux_world file, run

[[email protected] ~]# cat linux_world
Hi this is my first file in linux.
Linux always rocks
Thanks
[email protected] ~]#

Example 3) View the contents of multiple files with cat

To view the content of multiple files with cat command, then type cat followed the file names, example is shown below

[[email protected] ~]# cat linux_world linux_distributions /etc/fstab

Above command will display output of three files on the terminal.

Example 4) View contents of a file page wise

For example if we have a big file whose contents can’t be displayed at once on the screen , in that case we can use more and less command with cat to view the contents page wise.

[[email protected] ~]# cat /etc/passwd | more
[[email protected] ~]# cat /etc/passwd | less

Example 5) cat command without filename arguments

if we don’t specify any arguments in the cat command then it will read the inputs from the keyboard attached to the system. Type some text after entering the cat command.

[[email protected] ~]# cat
Ubuntu Linux Rocks at desktop Level

Now press ‘Ctrl-d‘ to inform cat that it has reached end of file (EOF). In this case it will display the line of text twice because it copies std input to std output.

[[email protected] ~]# cat
Ubuntu Linux Rocks at desktop Level
Ubuntu Linux Rocks at desktop Level
[[email protected] ~]#

Example 6) View the contents of a file with line numbers

Use ‘-n’ option in cat command to view contents of a file along with the line numbers.

[[email protected] ~]# cat -n linux_world
1 Hi this is my first file in linux.
2 Linux always rocks
3 Thanks
[[email protected] ~]#

In case, if your file has blank lines , then above command will also display the number of blank lines as well, so to remove the numbering of blank lines , we can use ‘-b‘ option in place of ‘-n’ in the above command.

Example 7) Copy the contents of one file to another with cat command

Using greater than ‘> symbol in cat command,  we can copy the contents of one file to another , example is shown below :

[[email protected] ~]# cat linux_world > linux_text
[[email protected] ~]#

In this case, if there are any contents in linux_text file then it will be overwritten by the contents of linux_world file

Example 8) Appending the contents of one file to another

Using double greater than symbol >> in cat command, we can append the contents of one file to another. Example is shown below :

[[email protected] ~]# cat /etc/passwd >> linux_text
[[email protected] ~]#

Above Command will append the contents of /etc/passwd file to linux_text file at the end. Use cat command to verify the contents of linux_text file.

cat-command-more

Example 9) Redirecting the output of multiple files into a single file

cat command can also be used to merge the contents of multiple files into a single file, example is shown below:

[[email protected] ~]# cat linux_world linux_distributions /etc/fstab > linux_merge_text

Above command will merge the output of 3 files into a single file ‘linux_merge_text’.

cat_merge_option

Example 10) Getting input using standard input operator

Use ‘<‘ symbol in cat command to get input from standard input operator.

[[email protected] ~]# cat < linux_distributions
RHEL
CentOS
Fedora
Ubuntu
SuSE
Linux Mint
[[email protected] ~]#

Above cat command is getting input from the file using std input operator ‘<‘

Example 11) Sorting the output of multiple files into a single file

cat command can also sort the contents of multiple files and merge it into a single file.By default sorting will done on the alphabetic order, if you want the sorting on the basis of numbers then use ‘-n’ option in the sort command.

[[email protected] ~]# cat linux_text linux_distributions /etc/passwd | sort > linux_sort

Example 12) Insert $ at end of each line with command

If you wish to append $ (dolar) symbol at end of each line in the file then use ‘-E’ option in cat command.

[[email protected] ~]# cat -E linux_world
Hi this is my first file in linux.$
Linux always rocks$
Thanks$
[[email protected] ~]#

As we can see in the above output, $ is appended at the end of each line.

Example 13) View the tab spaces in contents of a file

Let’s assume we have a file which has some tab spaces in the contents. To view the tab spaces in the contents of a file with cat command, use using -T option. Example is shown below:

Let’s create a file with some tab spaces.

cat-file-tab-space

Now display these tab spaces as ^I

cat-with-T-option

Example 14) Squeeze repeated blank lines with cat command

Use ‘-s’ option in cat command to suppress the repeated blank lines. When we use -s option in cat command then it only show one blank line and will squeeze repeated blank line.

Let’s take am example of file ‘linux_blank’ , which consists of multiple repeated blank lines.

file-with-blank-space

Now remove the repeated blank lines in the output using below command.

[[email protected] ~]# cat -s linux_blank 
test

test1
test2

test3

test4
[[email protected] ~]#

Example 15 ) View the contents in reverse order with cat command

tac is the reverse of cat command. tac will display the output in revers order example is shown below

[[email protected] ~]# tac linux_world
Thanks
Linux always rocks
Hi this is my first file in linux.
[[email protected] ~]#

Example 16 ) Display non-printing characters using -v option

-v option in the cat command is used to show the non-printing characters in the output. This option become useful when we are suspecting the CRLF ending lines, in that case it will show ^M at the end of each line.

[[email protected] tmp]# cat test_file
hi there
[[email protected] tmp]# cat -v test_file
hi there^M
[[email protected] tmp]#

That’s all from article and i hope these examples are informative to you. Please do share your feedback and Comments.

Read Also : 11 df command examples in Linux

6 Responses

  1. Parijatha Kumar says:

    The following command does not work.
    sudo cat hosts >> /etc/hosts.
    That’s why. I do …
    1) sudo su
    2) cat hosts >> /etc/hosts
    Is there a single command to achieve the result of the above two commands ?

  2. David says:

    Be careful, Examples 4 and 11 are winners of the “Useless use of cat award,” which is often the case when you pipe cat output to another command.

  3. danny says:

    how do you modify the cat command to show only the last paragraph of a textfile

Leave a Reply

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

Pin It on Pinterest