Linux Cat Command Examples
Linux command cat short for ‘concatenate’, is a very useful command. Using the cat command, you can create a file, view file content, concatenate files, and file output redirection. We will discuss the different uses of the cat command with some examples in this article.
Basic Syntax of Cat command
The following syntax is used for the cat command:
$ cat [options] [file-name]
File-name is the name of a file.
Use the following command to explore all cat options:
Print file content through the cat command
Using the cat command, you can display the file content on the terminal as follows:
For example, to view the content of the ‘test_file.txt’ file, type the below-mentioned command on the terminal:
The content of the above file will be displayed on the terminal.
Similarly, to display the content of multiple files, use the following command:
$ cat test_file.txt test_file1.txt
The above command will show you the content of the test_file.txt and test_file1.txt on the terminal.
File creation using the cat command
You can use the cat command to create a new file.
For example, we are creating a file with the name ‘new_filetest.txt’ by executing the following command:
Now, the user will input content into this file and then ‘Ctrl+d’ to leave this file. The content is written in ‘new_filetest.txt’ that you can display through the cat command.
Use of more and less options with cat command
If a file has a large content and you need to scroll to view more file content. In this case, use the following options with the cat command:
$ cat testfile.txt | less
Print line number with file content
Use the cat command along with option ‘-n’ to display the line number of file content as follows:
Display tab-separated characters
Use the option ‘-T’ and the cat command to display the tab-separated characters in a line.
In a line, the tab space will be filled with ‘^I’ character, which is also shown in the following screenshot:
Print ‘$’ at the End of Lines
To display the ‘$’ at the end of lines, use option ‘-e’ with cat command as follows:
The above option is useful when you want to shrink the multiple lines in a single line.
Redirect file content
Through the cat command, the user can redirect the standard output into a new file.
For example, to copy one file’s content into another file, you can use the cat command. We have a file with the name test_file.txt, and the other is test_file1.txt. So, to copy the content of ‘test_file.txt’ to a ‘test_file1.txt’, use the cat command with ‘>’ operator as follows:
$ cat test_file.txt > new_file.txt
If ‘test_file1.txt’ does not exist then, it will create a file with this name.
To append the content of ‘test_file.txt’ to a ‘test_file1.txt’, use the operator ‘>>’ in the cat command as follows:
$ cat test_file.txt >> test_file1.txt
Ignore the repeated empty lines
Using the cat command along with option ‘-s’, you can omit the empty lines from the output.
File concatenation using the cat command
The cat command is used to concatenate the file content. For example, the concatenate the content of test_file.txt and test_file1.txt and then write content into a new file mergefile.txt by using the ‘>’ operator as follows:
$ cat test_file.txt test_file1.txt > mergefile.txt
Conclusion
We have explained the Linux cat command with examples in this article. We have how the cat command can help a Linux user while he/she is working on a system. From the above examples, I hope you have learned a lot. Please give your feedback via comments.