15 Practise Examples of cat Command on Linux

Linux cat Command

cat stands for Concatenate.It is used to create a file and to display and modify the contents of a file. In this article we will discuss 15 different examples of “cat” command.

The general syntax is as follows:

cat <options> <filename>

Cat command Options as listed below;

catcmd

1.) Show the content of file.

Using below command, it will display the content of file.

#cat <filename>

[root@server1 ~]# cat /etc/group 
root:x:0: 
bin:x:1: 
daemon:x:2: 
sys:x:3: 
adm:x:4: 
tty:x:5: 
disk:x:6: 
lp:x:7: 
mem:x:8:

2.) Show Line Numbers in File.

Using “-n” option you could see the line numbers in file.

#cat -n <filename>

[root@server1 ~]# cat -b /etc/vsftpd/vsftpd.conf 
     1  # Example config file /etc/vsftpd/vsftpd.conf 
     2   
     3  # The default compiled in settings are fairly paranoid. This sample file 
     4  # loosens things up a bit, to make the ftp daemon more usable. 
     5  # Please see vsftpd.conf.5 for all compiled in defaults. 
     6   
     7  # READ THIS: This example file is NOT an exhaustive list of vsftpd options. 
     8  # Please read the vsftpd.conf.5 manual page to get a full idea of vsftpd's 
     9  # capabilities. 
    10   
    11  # Allow anonymous FTP? (Beware - allowed by default if you comment this out). 
    12  anonymous_enable=YES 
    13   
    14  # Uncomment this to allow local users to log in.

3.) Display the number of nonblank output lines in files.

Using “-b” option you could see the line numbers excluding the nonblank space.

#cat -b <filename>

[root@server1 ~]# cat -b /etc/vsftpd/vsftpd.conf  
     1    # Example config file /etc/vsftpd/vsftpd.conf 
 
     2    # The default compiled in settings are fairly paranoid. This sample file 
     3    # loosens things up a bit, to make the ftp daemon more usable. 
     4    # Please see vsftpd.conf.5 for all compiled in defaults. 
 
     5    # READ THIS: This example file is NOT an exhaustive list of vsftpd options. 
     6    # Please read the vsftpd.conf.5 manual page to get a full idea of vsftpd's 
     7    # capabilities. 
 
     8    # Allow anonymous FTP? (Beware - allowed by default if you comment this out). 
     9    anonymous_enable=YES 
 
    10    # Uncomment this to allow local users to log in.

Note: Difference between “-n” and “-b” options is that, “-b” will only show the number of non-blank lines and “-n” will show the all line numbers in file.

4.) Show Tab separated Lines in File.

#cat -T <filename>

Let’s create a file with some tab spaces.

[root@server1 ~]# cat linuxnew  
Linux is the future. 
    future for linux. 
        Ittroubleshooter 
[root@server1 ~]#

As shown below example, TAB space is filled up with ‘^I‘ character..

[root@server1 ~]# cat -T linuxnew  
Linux is the future. 
^Ifuture for linux. 
^I^IIttroubleshooter 
[root@server1 ~]#

5.) Display $ sign at end of each line.

Using “-E” option display the $ sign at the end of each line.

#cat -E <filename>

[root@server1 ~]# cat -E linuxnew  
Linux is the future.$ 
    future for linux.$ 
        Ittroubleshooter$ 
[root@server1 ~]#

6.) Equivalent to “-ET” option.

If you want to combine between “-T” and “-E”, you can use “-A” option.

#cat -A <filename>

[root@server1 ~]# cat -A linuxnew  
Linux is the future.$ 
^Ifuture for linux.$ 
^I^IIttroubleshooter$ 
[root@server1 ~]#

7.) More & Less Options to view the content per page.

When your file can not fit in your screen, you can combine cat with another command to make it displayed per page. Use the pipe ( | ) sign to combine it.

[root@server1 ~]# cat  /etc/vsftpd/vsftpd.conf  | less
[root@server1 ~]# cat  /etc/vsftpd/vsftpd.conf  | more

8.) Display the contents of 2 files.

When you want to view more than 1 file simultaneously, you can use below command format;

#cat <filename1> <filename2>

[root@server1 ~]# cat linuxnew linuxservers.doc  
Linux is the future. 
    future for linux. 
        Ittroubleshooter 
The content of linuxservers.doc file 
[root@server1 ~]#

9.) Sorting multiple files.

Use the cat command with another command to make a custom output. here’s the example;

[root@server1 ~]# cat linuxnew linuxservers.doc  | sort 
    future for linux. 
        Ittroubleshooter 
Linux is the future. 
The content of linuxservers.doc file 
[root@server1 ~]#

10.) Redirect the output to another file.

The below command will create a “testfile” file that has the same content with linuxnew and linuxservers.doc file.

[root@server1 ~]# cat linuxnew linuxservers.doc  > testfile 
[root@server1 ~]# cat testfile  
Linux is the future. 
    future for linux. 
        Ittroubleshooter 
The content of linuxservers.doc file 
[root@server1 ~]#

Note: >” sign (greater-than symbol) is used to  redirect the output into a file.

You can also redirect the multiple files into single file.

[root@server1 ~]# cat file1 
Redhat 
[root@server1 ~]# cat file2 
fedora 
[root@server1 ~]# cat file3 
ubuntu 
[root@server1 ~]# cat file1 file2 file3 > combinefile 
[root@server1 ~]# cat combinefile  
Redhat 
fedora 
ubuntu 
[root@server1 ~]#

11.) Create a New File.

There is numbers of ways to create a file. One of them is using cat command.

# cat > <filename>

[root@server1 ~]# cat > welcomefile 
Linux is the unix like OS. 
[root@server1 ~]# cat welcomefile  
Linux is the unix like OS. 
[root@server1 ~]#

Using above command it will create “welcomefile” file. Then you will see a blank line below the cat command (after execution of command). Put some data and  press ctrl+d (to save the file).

12.)  Appending the content of file.

Appends in existing file with ‘>>‘ (double greater than) symbol is used. Let’s see the example, the content of first file (i.e welcomefile) is copied to another file(i.e combinefile) with append the  existing file.

#cat <filename1>  >>  <filename2>

[root@server1 ~]# cat welcomefile >> combinefile  
[root@server1 ~]# cat combinefile  
Redhat 
fedora 
ubuntu 
Linux is the unix like OS. 
[root@server1 ~]#

13.) Redirect standard input using “<” sign.

# cat < <filename>

[root@server1 ~]# cat < combinefile  
Redhat 
fedora 
ubuntu 
Linux is the unix like OS. 
[root@server1 ~]#

As above example, Using redirect standard input (i.e “<“ sign) the content of the file (i.e) will be an input for “cat” command.

14.) Display the Contents in Reverse Order.

Using “tac” command you can display the output in revers order example is shown below.

#tac <filename>

[root@server1 ~]# tac combinefile  
Linux is the unix like OS. 
ubuntu 
fedora 
Redhat 
[root@server1 ~]#

15.) Create a file until a Specific Pattern.

In the below example, the block of lines are read from stdin (until HALT) and standard output is save on the file.

#cat <<HALT > <filename>

[root@server1 ~]# cat <<HALT > centosfile 
> Centos linux support most pieces modern PC hardware. 
> Linux is the multi-user and multi-tasking OS. 
> HALT 
[root@server1 ~]# cat centosfile  
Centos linux support most pieces modern PC hardware. 
Linux is the multi-user and multi-tasking OS. 
[root@server1 ~]#

Hope this post will help Linux/Unix beginners. Please share you feedback and Comments.!!!

Leave a Reply

Be the First to Comment!

Notify of
avatar
wpDiscuz