This tutorial explains Linux “cat” command, options and its usage with examples.
The cat command in Linux is frequently used for concatenating files and displaying their contents on the standard output. It is considered one of the most frequently used commands on Linux and UNIX-like operating systems.
Syntax:
cat [OPTION]... [FILE]...
The “cat” command serves various purposes in UNIX or Linux, including:
- Copy Text Files: It can be used to display and copy the content of text files.
- Create and Combine Text Files: You can create and combine text files by using the “cat” command to merge the content of multiple files into one.
- Display Files on the Screen: It is commonly used to display the content of files directly on the terminal screen.
cat Command Examples
Here are some examples of how the cat command is used:
Example 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>
Example 2: Displaying the Contents of Multiple Files Using ‘cat’ (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; }
Example 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
Example 4: Displaying File Contents with Line Numbers (cat -n file_name)
You can use the ‘-n’ option with ‘cat’ to display file content 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 .
Example 5: Making Backups of Files (cat original_file_name > backup_file_name):
You can create backups of files by copying their content to another file using cat command.
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
Example 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 .
Example 7: Displaying 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.
Example 8: Display Tab Character in a Line of a File (cat -neT file_name):
To show tab characters in a line, you can use the ‘-neT’ option.
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
Example 9: Print the File Contents in a Reverse Order (tac file_name):
This can be achieved 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>
Example 10: Reading a File Up to 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
Sanfoundry Global Education & Learning Series – 1000 Linux Tutorials.
If you find any mistake above, kindly email to [email protected]
- Check Linux Books
- Check Information Technology Books
- Apply for Programming Internship
- Practice Programming MCQs