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.
cat OPTIONS FILE_NAME
- 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 is will display the end of the file.
- n: This option will give a number to each line is 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 in CAT Command in Linux
Let us discuss with the example to clarify our understanding:
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.
Cat > Testfile.txt
After adding the content press Cntrl + D to exit the shell and write the content to file.
How to Display the Content of a File: In order to show the content of the file, we can use 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.
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.
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.
cat Testfile.txt > File_1.txt
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.
The file Location.txt file also contains two lines:
** Now we will use shell redirection(>>) to append the content of the two files:
cat Shop.txt >> Location.txt
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.
cat *.txt > merge.txt
You can remember the content combined from Testfile.txt,File_1.txt,Shop.txt and Location.txt as all have the .txt extension.
Adding Line Number to the Cat Output: To add line number to the output of cat command we will use the -n option. It will prepend the numerical value to the start of each line.
cat -n merge.txt
You can see the numbers are given to each line in ascending order.
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.
cat -E merge.txt
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 “$”
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
cat -s line.txt
We can see that duplicate records have been removed.
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
cat -T tabs.txt
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 –