How to use cat command in linux

Table of contents

Introduction

This article explores various use cases of the Cat command with examples 

To read and display the contents of file you can use the cat command with the following syntax 

# cat file

Following file is used to demonstrate the different options available to use with cat command 

# In the below file empty lines are used to demonstrate some use cases of cat command 

[root@discoveringsystems-centos ~]# cat discovering-systems.txt
this is a example text to demo the use cases of the cat
this file is named as discovering-systems.txt





this is the fourth line
here is the fifth line
finally 6th line

To view all options available to use with Cat command you can use the “–help” option along with it 

[root@discoveringsystems-centos ~]# cat --help
Usage: cat [OPTION]... [FILE]...
Concatenate FILE(s), or standard input, to standard output.
.
.< output trimmed for brevity >
.

How to add sequence number to the lines present on a text file with cat command 

Here in this example I am numbering all non empty lines  using the option “-b” along with cat command 

[root@discoveringsystems-centos ~]# cat -b discovering-systems.txt
     1  this is a example text to demo the use cases of the cat
     2  this file is named as discovering-systems.txt





     3  this is the fourth line
     4  here is the fifth line
     5  finally 6th line

Here in this example , I am numbering all the lines including the empty lines using the option “-n” along with cat command

[root@discoveringsystems-centos ~]# cat -n discovering-systems.txt
     1  this is a example text to demo the use cases of the cat
     2  this file is named as discovering-systems.txt
     3
     4
     5
     6
     7
     8  this is the fourth line
     9  here is the fifth line
    10  finally 6th line

How to use cat command to squeeze the empty lines 

The original file had many empty lines and it got squeezed into a single empty line and then displayed while using cat command with option “-s” 

[root@discoveringsystems-centos ~]# cat -s discovering-systems.txt
this is a example text to demo the use cases of the cat
this file is named as discovering-systems.txt

this is the fourth line
here is the fifth line
finally 6th line

How to mark  the end of line with cat command 

We can mark the end of line with “$” by using option “-E” along with cat command 

#original text 
[root@discoveringsystems-centos ~]# cat discovering-systems.txt
this is a example text to demo the use cases of the cat
this file is named as discovering-systems.txt





this is the fourth line
here is the fifth line
finally 6th line

# end of line marked text 
[root@discoveringsystems-centos ~]# cat -E discovering-systems.txt
this is a example text to demo the use cases of the cat $
this file is named as discovering-systems.txt  $
        $
$
$
$
$
this is the fourth line $
here is the fifth line $
finally 6th line $

How to display the TAB characters using the cat command

We can make the TAB character visible on the file by using the option “-T” along with the cat command . The TAB characters will show up as “^I” when displayed

#original text 
[root@discoveringsystems-centos ~]# cat discovering-systems.txt
this is a example text to demo the use cases of the cat
this file is named as discovering-systems.txt





this is the fourth line
here is the fifth line
finally 6th line

#TAB characters made visible 
[root@discoveringsystems-centos ~]# cat -T discovering-systems.txt
this is a example text to demo the use cases of the cat
this file is named as discovering-systems.txt
^I




this is the fourth line
here is the fifth line
finally 6th line

How to mark the end of line and display TAB characters at the same time using cat command 

In the following example , we will mark the end of line with characters “$” and also make the TAB characters visible by using “^I”. To make this happen we can use the option “-A “ along with cat command

[root@discoveringsystems-centos ~]# cat -A discovering-systems.txt
this is a example text to demo the use cases of the cat $
this file is named as discovering-systems.txt  $
^I$
$
$
$
$
this is the fourth line $
here is the fifth line $
finally 6th line $

Or ( both does the similar job )

[root@discoveringsystems-centos ~]# cat -vET discovering-systems.txt
this is a example text to demo the use cases of the cat $
this file is named as discovering-systems.txt  $
^I$
$
$
$
$
this is the fourth line $
here is the fifth line $
finally 6th line $

How to use cat command to create an empty file

In this example I am creating a file named as emptyfile with cat and redirection

[root@discoveringsystems-centos ~]# cat > emptyfile

^C
[root@discoveringsystems-centos ~]# cat emptyfile

How to use cat command to copy the contents of one file to another 

In the following example , I am reading the contents of the discovering-systems.tx file and redirecting it to the emptyfile which was already created . By doing this I am performing copy like action for one file contents to over another file

[root@discoveringsystems-centos ~]# cat discovering-systems.txt > emptyfile

[root@discoveringsystems-centos ~]# cat emptyfile
this is a example text to demo the use cases of the cat
this file is named as discovering-systems.txt
this is the fourth line
here is the fifth line
finally 6th line

How to use cat command to append new contents to the file 

Using “>>” we can append more contents to the another file while reading from a file using cat command 

[root@discoveringsystems-centos ~]# cat discovering-systems.txt >> emptyfile
[root@discoveringsystems-centos ~]# cat emptyfile
this is a example text to demo the use cases of the cat
this file is named as discovering-systems.txt
this is the fourth line
here is the fifth line
finally 6th line
this is a example text to demo the use cases of the cat
this file is named as discovering-systems.txt
this is the fourth line
here is the fifth line
finally 6th line

How to use Tac command to show the reverse of the file 

We can also reverse the displayed contents of the file using the tac command. Tac command working is similar when compared to cat command but reverses the lines 

#Original 
[root@discoveringsystems-centos ~]# cat emptyfile
this is a example text to demo the use cases of the cat
this file is named as discovering-systems.txt
this is the fourth line
here is the fifth line
finally 6th line

#reverse 
[root@discoveringsystems-centos ~]# tac emptyfile
finally 6th line
here is the fifth line
this is the fourth line
this file is named as discovering-systems.txt
this is a example text to demo the use cases of the cat

How to use cat command to deal with multiple files at the same time 

Cat command can deal with multiple files in a single command .In the below example we have 3 files present ( firstfile.txt , secondfile.txt, thirdfile.text ). We  used the cat command to read them individually or all together at once 

[root@discoveringsystems-centos ~]# ls -ltrh *file.text
-rw-r--r--. 1 root root 20 Dec 21 09:37 firstfile.text
-rw-r--r--. 1 root root 21 Dec 21 09:38 secondfile.text
-rw-r--r--. 1 root root 20 Dec 21 09:38 thirdfile.text

How to use cat to read files individually

[root@discoveringsystems-centos ~]# cat firstfile.text
first file contents
[root@discoveringsystems-centos ~]# cat secondfile.text
second file contents
[root@discoveringsystems-centos ~]# cat thirdfile.text
third file contents

 How to use cat to display contents of  all the 3 files at once

[root@discoveringsystems-centos ~]# cat  *file.text
first file contents
second file contents
third file contents

Or ( both does the similar job ) 

[root@discoveringsystems-centos ~]# cat firstfile.text secondfile.text thirdfile.text
first file contents
second file contents
third file contents

How to use cat to read and merge all the file contents onto a single file

[root@discoveringsystems-centos ~]# cat firstfile.text secondfile.text thirdfile.text  > fourthmergefile.text
[root@discoveringsystems-centos ~]# cat fourthmergefile.text
first file contents
second file contents
third file contents

Or ( both does the similar job )

[root@discoveringsystems-centos ~]# cat  *file.text > fourthmergefile.text
[root@discoveringsystems-centos ~]# cat fourthmergefile.text
first file contents
second file contents
third file contents

Leave a Comment

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