- How to Use the Cat Command in Linux [22 Useful Examples]
- Cat Command Syntax
- 1. Display Contents of a File in Linux
- 2. Display Contents of Multiple Files in Linux
- 3. Create a File with Cat Command
- 4. Append Text to a File in Linux
- 5. Copy File Content to Another File in Linux
- 6. Append Contents of Multiple Files Into One File on Linux
- 7. View File Content with Line Endings
- 8. List Contents of All Specified File Types
- 9. Display Line Numbers in File
- 10. Print Line Numbers of Multiple Files
- 11. Show File Contents with Tab Characters
- 12. View File Contents with More Command
- 13. View File Contents with Less Command
- 14. Suppress Repeated Empty Lines in Output
- 15. Append File Content to End of Another File
- 16. Display File Contents in Reversed Order
- 17. Show File Content in Binary Format
- 18. Show File Content in Hexadecimal Format
- 19. Display Specific Lines of File in Linux
- 20. Sort the Contents of File Alphabetically
- 21. Display End of File Marker in File
- 22. View CPU Info in Linux
How to Use the Cat Command in Linux [22 Useful Examples]
The cat (short for “concatenate“) command is one of the most frequently used commands in Linux that comes pre-installed in most Linux distribution systems and is primarily used to display the content of existing files.
Moreover, the cat command can be utilized by the user to concatenate multiple files, create new files, append content to existing files, view the content of a file, and redirect output in the terminal or files.
The cat command can also be used to format the output of the file with the help of different options, such as adding numbers before each line of the file’s content.
Additionally, it can execute in combination with other commands to perform various tasks including providing page navigation and conversion of file format to binary or hexadecimal.
In this article, we are going to find out the handy use of cat commands with their examples in Linux.
Cat Command Syntax
The cat command can accept multiple options and file name arguments as shown:
Let’s understand the above syntax:
- [OPTION] – Users can provide multiple options to alter the behavior of the command. The options start with a hyphen («-«) , such as «-E» is used to display line ends and «-n» to display numbers before lines.
- [FILE] – The file argument specifies the file which will be manipulated by the command. However, users can provide names of multiple files separated by space.
Note: To read in detail about all the available options of the cat command, execute the «cat —help» command in your Linux terminal:
Let’s explore different examples to harness the power of cat command.
1. Display Contents of a File in Linux
The basic functionality of the cat command is to display the content of an existing file in Linux. For that purpose, provide the name of the file with no option as shown.
Here in the command, the content of the file “tecmint1.txt” which is located in the “Documents” directory will display.
2. Display Contents of Multiple Files in Linux
The cat command can also be utilized to show the contents of more than one file by providing the file names separated by space as shown:
$ cat tecmint1.txt tecmint2.txt
In the above output, we can see the contents of both files in the terminal. The first two lines are of file “tecmint1.txt”, whereas the last line of the output is the content of the “tecmint2.txt” file.
3. Create a File with Cat Command
The user can create a new file and save content in it with the «>» symbol (known as the “output redirection operator”) will redirect the output of the command to the file specified by the filename “Tecmint_tutorial.txt” as shown.
Note: If you want to create a new file, be careful that a file with the same name does not exist already. Otherwise, the command will overwrite the content of the existing file.
After executing the command, an indicator will blink in the new line. Write the content for the file and press the “CTRL + D” keys to save and exit the file:
You can verify the file’s creation by using the ls command and use the cat command to view the content of the newly created file:
$ ls $ cat Tecmint_tutorial.txt
4. Append Text to a File in Linux
One of the benefits of the cat command is that it can append the content to an existing file using the «>>» symbol (known as “append redirection operator”) will append/combine the additional content to an existing file “Tecmint_tutorial.txt“.
Type or paste the content that you want to append to this file and press the «CTRL + D» keys:
Now let’s verify if the content has been appended to the existing contents stored in the file:
5. Copy File Content to Another File in Linux
Sometimes, the user wants to create a copy of the contents stored in a file into a new file for different purposes, such as backup. Here, «>» operator will read the content of the “Tecmint_tutorial.txt” file sequentially and will place it into a new file named “New_file.txt“.
$ cat Tecmint_tutorial.txt > New_file.txt
The next step is that you verify if the new file has been created successfully by running:
6. Append Contents of Multiple Files Into One File on Linux
As mentioned earlier, the cat command can be utilized for concatenation purposes. Let’s run the command to concatenate/merge contents of the “tecmint1.txt” and “tecmint2.txt” files and store the result in a new file named “cat_tecmint.txt”:
$ cat tecmint1.txt tecmint2.txt > cat_tecmint.txt
The above command will read the content of the “tecmint1.txt” and “tecmint2.txt” files and will write them in a new file “cat_tecmint.txt”.
Moving forward, we need to verify whether the new file stores the concatenated content of both files or not:
7. View File Content with Line Endings
The user can also use the «-E» option to view the EOL (End of Line) character in the contents of the file. The EOL characters are known as non-printing characters and they are represented by the dollar («$») symbol.
The expected output will show the «$» symbol at the end of each line of the content.
8. List Contents of All Specified File Types
The cat command can use the «*» wildcard character to list the content of all files available in the current directory. Additionally, you can also specify any particular file type such as «.txt» followed by a wildcard character to show the content of all “txt” files available in the directory.
The output portrays the content of all “txt” files one after one.
9. Display Line Numbers in File
If you want to show line numbers prior to each line of the file’s content, use the «-n» option to display the line number in the output without any changes to the original content of the file.
The expected output will show line numbers before each line.
10. Print Line Numbers of Multiple Files
The cat command with the «-n» option can also work on multiple files by concatenating the contents of multiple files and adding numbers prior to each line of combined output.
Here in the command, the content “Fruits.txt” and “veg.txt” files will combine, and then the «-n» option will add line numbers at the beginning of each line of the output.
11. Show File Contents with Tab Characters
The «-T» option can display the tab spaces characters «^I» in the output which are known as non-printing characters.
The output contains the tab characters in place of tab spaces.
12. View File Contents with More Command
Some files contain a lot of content that does not entirely fit in the output screen of the terminal. If we use the simple cat command to display the content of such files, the output does not indicate that more content is available and the user needs to scroll down to see it.
Here in the output, we can see only some of the actual content.
To resolve this issue, you can use the pipe «|» symbol that helps in using the output of one command as the input to another command, in this case, it is the “more” command that offers page navigation at the end of the file.
13. View File Contents with Less Command
You can also use less commands to view the contents of a file in a scrollable and searchable manner using the keys.
14. Suppress Repeated Empty Lines in Output
In some cases, the user leaves repeated empty lines by mistake instead of a single empty line. However, the cat command can be utilized to suppress repeated empty lines from the content of a file with the help of the «-s» option.
The output has only single empty lines, all repeated lines are suppressed successfully.
15. Append File Content to End of Another File
The cat command can append the content of a file at the end of another file by using the «>>» symbol (known as “append redirection operator”).
$ cat Fruits.txt >> veg.txt $ cat veg.txt
The output shows that the contents of both files are appended in the “veg.txt” file.
16. Display File Contents in Reversed Order
To display the file’s content in reverse order, use the tac command, which is also known as ‘cat’ backward that displays the last line first, then the second last, and so on.
The output displays the content of the “Weekdays.txt” in reverse order.
17. Show File Content in Binary Format
The cat command can be used in combination with the “xxd” utility along with the «-b» option to convert the contents of the file into the binary format.
Let’s break down the above command:
- | – The pipe symbol (|) will give the output of the cat command to the command (xxd -b).
- xxd – It is a utility that will convert content into a hexadecimal representation.
- -b – This option is used with xxd to specify the binary output format instead of the default hexadecimal format.
The output portrays the file’s content in binary format and the original format side by side.
18. Show File Content in Hexadecimal Format
To convert the content of a file into the hexadecimal format, the user can utilize the “hexdump” utility as shown below:
$ cat Weekdays.txt | hexdump -C
Here in the command, the pipe symbol is joining both commands whereas the “hexdump” command will convert the content into hexadecimal format. Additionally, the «-C» option will show the ASCII representation alongside the hexadecimal values.
The output shows the converted content of the file in hexadecimal format successfully.
19. Display Specific Lines of File in Linux
The cat command can be combined with the sed command to display a specific range of lines from a file that matches the defined pattern from the file.
$ cat Weekdays.txt | sed -n '3,6p'
Here in the above command, the sed command will get the output of the cat command as input with the help of the pipe symbol. Then the sed command with the option «-n» and pattern “3,6p” will print lines 3 to 6 from that input.
20. Sort the Contents of File Alphabetically
The user can utilize the cat command with the sort command to alphabetically sort the lines of content as shown.
The output displays the alphabetically sorted content of the file.
21. Display End of File Marker in File
The cat command can be utilized with the “here document” which aids the user to input the content into the file and set the page end marker.
The input process is terminated by entering the specified delimiter on a new line.
The output saved the input in the file “month.txt” and terminated the input process when “EOF” was entered.
22. View CPU Info in Linux
The cat command can also display the content of a virtual file named “cpuinfo”, which contains information about the CPU processor, model name, cache size, number of cores, and other details of the CPU.
That is it for this blog, I hope you understood the usage of the cat command in Linux. You may refer man page of the cat command if you want to know more options.
Conclusion
This article displayed 20 examples of the cat command in Linux to concatenate, display, and create files. Additionally, the cat command can be utilized with other Linux commands to perform more advanced operations like sorting content or converting the content into other file formats.