This tutorial explains Linux “cat” command, options and its usage with examples.
The cat command is considered as one of the most frequently used commands on Linux or UNIX like operating systems.
Usage: cat [OPTION]… [FILE]…
cat command can be used for the following purposes under UNIX or Linux:
* Copy the text-files
* Create and combine text files
* Show the files on the screen
Here’s the listing of example usage of cat command :
1. Display the contents of a file on the screen(cat file_name):
sanfoundry-> cat 1.html <!DOCTYPE html> <html> <head> <style> div.ex { width:450px; padding:10px; border:5px solid gray; margin:0px; background:#C8C8C8; } </style> </head>
2. To display the contents of more than one file(cat file_name1 file_name2….):
sanfoundry-> cat 1.html 2.c <!DOCTYPE html> <html> <head> <style> div.ex { width:450px; padding:10px; background:#C8C8C8; } </style> </head> #include<stdio.h> int main() { int a , b ; scanf("%d %d",&a,&b); printf("sum==%d\n",a + b); return 0; }
By default cat command displays output on the screen but with “>” operator you can redirect output to other file.
sanfoundry-> cat 2.c > 3.txt
Here you can see that contents of the 2.c is redirected to the file 3.txt and it is not directly displayed on the screen. Again you can do cat 3.txt to see the contents in it.
sanfoundry-> cat 3.txt #include<stdio.h> int main() { int a , b ; scanf("%d %d",&a,&b); printf("sum==%d\n",a + b); return 0; }
3. To create a New file(cat > file_name):
You can also redirect the stdout to a new file with “>” operator.
sanfoundry-> cat > 1.txt Hi my name is x . I am friend of y sanfoundry-> cat 1.txt Hi my name is x . I am friend of y
You can use “>>” operator to append symbol to append some contents in the file.
sanfoundry-> cat >> 1.txt Hi I am friend of x and y both sanfoundry-> cat 1.txt Hi my name is x . I am friend of y Hi I am friend of x and y both
4. Display contents of file with line numbers(cat -n file_name):
-n option helps to print the output with line numbers.
sanfoundry-> cat -n 1.txt 1 Hi my name is x . 2 I am friend of y 3 Hi I am friend of x and y both 4 Hi i am z 5 6 7 Hi i am w
Here you can notice that empty lines are also numbered. So if you have to find out the exact number of filled lines
instead of -n use -b option:
sanfoundry-> cat -b 1.txt 1 Hi my name is x . 2 I am friend of y 3 Hi I am friend of x and y both 4 Hi i am z
Note
-b option only works with GNU cat command version .
5. Make backups of the files(cat original_file_name > backup_file_name):
You can use cat command to copy contents of a file into another so as to make backup of the file.
sanfoundry-> cat 1.txt > 1_backup.txt sanfoundry-> cat 1_backup.txt Hi my name is x . I am friend of y Hi I am friend of x and y both Hi i am z
6. Print repeated empty lines only once(cat -ns file_name):
If you want to print the repeated empty lines in a file only once in the stdout so as to enhance the space compatibility -s option is helpful.
sanfoundry-> cat -ns 1.txt 1 Hi my name is x . 2 I am friend of y 3 Hi I am friend of x and y both 4 Hi i am z 5 6 Hi i am w
Here from output of example 5 you can see line numbers 5, 6 are empty, But here that lines were combined and displayed as only one line that is line number 5 .
7. Display end of every line in a file(cat -ne file_name):
Sometimes you are unable to determine the number of whitespaces in a line. so you can use -e option to print “$” at the end of each line.
sanfoundry-> cat -ne 1.txt 1 Hi my name is x . $ 2 I am friend of y $ 3 Hi I am friend of x and y both$ 4 Hi i am z $ 5 $ 6 $ 7 Hi i am w $
Here you can see line 5 though seems to be empty, It is not Since it contents a bunch of whitespace characters and each line is ended with “$” sign.
8. Display Tab character in a line of a file(cat -neT file_name):
Use -T option alongwith -e for this purpose.
sanfoundry-> cat -neT 1.txt 1 Hi my name is x . $ 2 I am friend of y $ 3 Hi I am friend of x and y both$ 4 Hi i am z $ 5 $ 6 $ 7 Hi i am w$ 8 ^I$ 9 $
Here in line 8, You can see “^I” which indicates presence of Tab character in that line.
Note
To display all nonprinting characters,except for tabs and the end of line Use :
$ cat -v file_name
9. Print the file contents in a reverse order(tac file_name):
This can be acieved using reverse of the “cat” that is “tac” command.
sanfoundry-> tac 2.c } return 0; printf("sum==%d\n",a + b); scanf("%d %d",&a,&b); int a , b ; { int main() #include<stdio.h>
10. Read the file upto a specific character(cat << pattern): You can use cat << "pattern". This Pattern works like End Of Inputs and stop reading from stdin and displays the contents above it.
sanfoundry-> cat << END > Hi i am x > Hi i am y > How are you ? > I am Fine ? > END Hi i am x Hi i am y How are you ? I am Fine ?
Note :
useless use of cat command
The purpose of cat is to concatenate (or “catenate”) files. If it’s only one file, concatenating it with nothing at all is a waste of time, and costs you a process.
cat file_name | sed -e 'commands' -e
The above command can be used as follows :
sed sed -e 'commands' file_name