Updated March 20, 2023
Introduction to CAT Command in Linux
The cat command is termed “concatenate” which is one of the widely used commands in Unix/Linux & Apple Mac OS operating system. This command has different applications like create single/ multiple files, view the content of the file, concatenate file and redirect their output to the terminal or file. The cat command displays the file content to a screen. Cat command concatenates standard inputs to the standard output. When there is no FILE or when FILE is – then it read the standard input? It performs three main roles related to manipulation of text files: creating them, displaying them & combining them.
Syntax:
cat OPTIONS FILE_NAME
Options:
- A: It is used to show all the content of the file.
- b: This option is used to give a number with nonblank/nonempty output lines and overrides the option -n.
- e: It is almost equal to option -vE.
- E: It will display the end of the file.
- n: This option will give a number to each line the file in ascending order.
- s: This option will suppress the multiple empty output lines.
- t: This option is similar to option -vT.
- T: It is used to display the special Tab characters like ^I.
- u: It is used to ignore the things.
- v: It is used to show non-printable characters used ^ and M notations.
- help: This option is used to get detailed information about the cat command.
- version: Using this option we can know the version information of cat command.
Examples to Implement CAT Command in Linux
Given below are the examples mentioned:
Example #1
Create a New File.
Using Cat command we can easily create a file. Let us create a file with name Testfile.txt and write the content “This is my First command in Linux.
Code:
Cat > Testfile.txt
After adding the content press Cntrl + D to exit the shell and write the content to the file.
Output:
Example #2
How to Display the Content of a File.
In order to show the content of the file, we can use the cat command simply and pass the name of the file/files which we want to view. The output of the file will be printed on the standard output and can be viewed on the terminal.
Code:
cat Testfile.txt
Output:
If the contents of a file are very long, then also all the content will be written to the terminal. In such cases, it will be challenging to discover or identify all the contents. When searching for particular content grep will be a good option.
Example #3
Writing the Content of Any File to a New File.
In order to write the contents of a file to a new file “shell redirection” will be used with the cat command. In the below example suppose that the file Testfile.txt has a single line with ‘This is my First Command in Linux’ and write this into File_1.txt.
Code:
cat Testfile.txt > File_1.txt
cat File_1.txt
Output:
Example #4
Append the Content of a File to Another File.
If we need to append the content of any file to another file, the shell redirection will be used. For example, Consider we have two files Shop.txt and Location.txt. The file Shop.txt contains two lines.
Code:
cat Shop.txt
Retail Shop
Wine Shop
The file Location.txt file also contains two lines:
Code:
cat Location.txt
Manali,HP
Indore,MP
** Now we will use shell redirection(>>) to append the content of the two files:
Code:
cat Shop.txt >> Location.txt
Code:
cat Location.txt
Output:
Example #5
Merging of Multiple Files Into Unified File.
In order to combine multiple files into a single file, cat command will be used with shell redirection. The below-mentioned command will combine all files with the *.txt extension inside a directory into one file in the same or different directory.
Code:
cat *.txt > merge.txt
Code:
cat merge.txt
Output:
You can remember the content combined from Testfile.txt,File_1.txt,Shop.txt and Location.txt as all have the .txt extension.
Example #6
Adding Line Number to the Cat Output.
To add line number to the output of the cat command we will use the -n option. It will prepend the numerical value to the start of each line.
Code:
cat -n merge.txt
Output:
You can see the numbers are given to each line in ascending order.
Example #7
Showing the End of the Line.
If we want to know the end of lines in a file then we will use cat command with the -E option. It will append a “$” character to the end of the lines. This option is useful to check when there is trailing space in the file. For example, let us use the same merge.txt.
Code:
cat -E merge.txt
Output 1:
In the above output, there is no space at the end of the line.
Now let us add some lines with space then we will see a tab space between the end line and “$”.
Output 2:
Example #8
Squeezing or Compressing of Blank Lines.
In order to squeeze/compress blank lines in a file, we can use a cat with the -s option. It will squeeze the blank line then suppress repeating empty output lines.
Suppose we have file line.txt, which contains the below data:
- File is squeezed
- File is compresses
- File is suppressed
- File is squeezed
Code:
cat -s line.txt
Output:
We can see that duplicate records have been removed.
Example #9
Showing Tabs in the File.
To show tab we can use cat with -T option. It will show tabs as ^I.
File tabs.txt contains:
- 1 tab
- 2 tab
Code:
cat -T tabs.txt
Output:
Example #10
Version.
Code:
cat --version
Output:
Recommended Articles
This is a guide to CAT Command in Linux. Here we discuss the syntax and 10 different examples to implement cat command in Linux with proper codes and output. You can also go through our other related articles to learn more –