cat command in Linux / Unix




Linux cat command.

cat  command is used to show the content of a files and also to combine several files together in one file.

The cat command does not accept directories as input.

cat command syntax

cat filename [-n] [-b] [-u] [-s] [-v] Allows look, modifying or combining a file

cat [file.txt] [file2.txt] > [file3.txt] Reads file1.txt and file2.txt and combines those files to make file3.txt

cat command options

cat command main options:

option description
cat dumps the file content on terminal
cat -n displays line numbers to all lines of displayed content
cat -b displays line numbers to non blank lines of displayed content
cat -E displays $ at the end of line of displayed content
cat -s replaces blank lines to one line
cat -T displays ^I instead of tabs
cat [file.txt] [file2.txt] .. dumps the content from all files together on terminal
cat [file.txt] [file2.txt] > [file3.txt] Reads file1.txt and file2.txt and combines those files to make file3.txt

cat command examples

For exampe we have a file named sample.txt which contains the following content :

sample.txt

Sample text line 1
Sample text line 2 after 1 line space   	with tabs	newx tab	next tab
Sample text line 3 after 2 line space
Sample text line 4 after 2 more line space
Sample text line 5
Sample text line 6

Now let us practice some cat commands with this file.

 

cat – View text file data:

$ cat sample.txt 
Sample text line 1
Sample text line 2 after 1 line space   	with tabs	newx tab	next tab
Sample text line 3 after 2 line space
Sample text line 4 after 2 more line space
Sample text line 5
Sample text line 6
$

 


cat -n – displays line numbers to all lines of displayed content:

$ cat -n sample.txt 
     1	Sample text line 1
     2	
     3	Sample text line 2 after 1 line space   	with tabs	newx tab	next tab
     4	
     5	
     6	Sample text line 3 after 2 line space
     7	
     8	
     9	Sample text line 4 after 2 more line space
    10	Sample text line 5
    11	Sample text line 6
$

 


cat -b – displays line numbers to non blank lines of displayed contentt:

$ cat -b sample.txt 
     1	Sample text line 1
     2	Sample text line 2 after 1 line space   	with tabs	newx tab	next tab
     3	Sample text line 3 after 2 line space
     4	Sample text line 4 after 2 more line space
     5	Sample text line 5
     6	Sample text line 6
$

 


cat -E – displays $ at the end of line of displayed content:

$ cat -E sample.txt 
Sample text line 1$
$
Sample text line 2 after 1 line space   	with tabs	newx tab	next tab$
$
$
Sample text line 3 after 2 line space$
$
$
Sample text line 4 after 2 more line space$
Sample text line 5$
Sample text line 6$
$

 


cat -s – replaces blank lines to one line:

$ cat -n sample.txt 
Sample text line 1
Sample text line 2 after 1 line space   	with tabs	newx tab	next tab
Sample text line 3 after 2 line space
Sample text line 4 after 2 more line space
Sample text line 5
Sample text line 6
$

 


cat -T – displays ^I instead of tabs:

$ cat -n sample.txt 
Sample text line 1
Sample text line 2 after 1 line space   ^Iwith tabs^Inewx tab^Inext tab
Sample text line 3 after 2 line space
Sample text line 4 after 2 more line space
Sample text line 5
Sample text line 6
$

 


cat [file.txt] [file2.txt] > [file3.txt]> – Reads file1.txt and file2.txt and combines those files to make file3.txt:

Let us say we have two text files sample.txt and sample2.txt.

First let us see the content of both the files

$ cat sample.txt 
Sample text1 line 1
$
$ cat sample2.txt 
Sample text2 line 1
$

Now to display the content of both the file we can give the following command. The result will be the dump of both the files.

$ cat sample.txt sample2.txt 
Sample text1 line 1
Sample text2 line 1
$

 

Now to combine the content of these two files and save the combined content to the third file, give the following commands.

$ cat sample.txt sample2.txt > sample3.txt
$ cat sample3.txt  
Sample text1 line 1
Sample text2 line 1
$

 


Partner Sites

VideoToGifs.com

EasyOnlineConverter.com

SqliteTutorials.com





Be the first to comment

Leave a Reply

Your email address will not be published.


*