The cat command, short for concatenate, is one of the most commonly used commands in Linux. It is primarily used to concatenate and display the contents of files. It can also be used to create, modify, or combine files.
The cat command is normally used to view short files or combine files. Basically, it can provide a quick display of the entire file.
When comes to interactive navigation or searching within the file or large file, cat command is not recommended. As its dumps entire file content to the screen at once - not convenient if you want to read through it.
2. Syntax and Options
The cat command takes the following command syntax:
cat options [filename] ...
The following table shows common options for cat:
Options
Description
-b or --number-nonblank
Number the non-blank output lines, starting at 1.
-E or --show-ends
Display a dollar sign $ at the end of each line before the newline.
-v or --show-nonprinting
Display non-printing characters in caret notation (^) and M- notation, except for the LFD (Line Feed) and TAB characters
-e
Equivalent to -vE
-T or --show-tabs
Display TAB characters as ^I
-A or --show-all
Equivalent to -vET
-n or --number
Numbers all output lines starting at 1.
-s or --squeeze-blank
Replace multiple adjacent empty lines with a single empty line in the output
--help
Display help message
--version
Prints version information and exit
3. Displaying File Contents
To display the content of a file type the cat command followed by the name of file(s).
cat myfile1.txt
This command outputs the content of the file named myfile1.txt to the terminal.
For large files, the only option to navigate the file content is to scroll up using the mouse.
If you wish to view the contents of multiple text files at a go, simply specify the files in one line separated by a space. In this example, we have displayed the contents of two files, one after the other.
cat myfile1.txt myfile2.txt
4. Create a new file
Cat command allows you to create a file and quickly add content to it.
Example:
To create a file, use cat > filename, then press ENTER. Then
cat > myfile1.txt
To create a file, use cat > filename, then press ENTER. Then on the new line add file contents. Once file content is added, Press ENTER and then press Ctrl+D to save the file. Here cat command reads the input and writes it on the file.
You can also create a file in the command line or a script:
cat > flowers.txt << EOF
rose
tulip
lily
EOF
The file content is the lines between the << EOF and EOF. The flowers.txt file will contain the following lines:
apple
orange
lemon
If flowers.txt did not exist before, it will be created. If it existed before, it will be overwritten.
5. Concatenating Files
Yes, one of the main purposes of the cat command (short for concatenate) is to concatenate and display the content of files. Let's look into a few different ways to do it.
5.1. Redirect contents of one file to another file
Using the redirection operator or greater-than sign > you can redirect contents from the standard input file to another text file. In this example, we have redirected or copied the contents of file 1 which is myfile1.txt to file 2 which is myfile3.txt.
cat myfile1.txt > myfile3.txt
You can, thereafter, display the contents of the destination file using the following cat command.
cat myfile3.txt
If the destination filename is not present, a new file is created upon running the command. In addition, if the destination file contains some data, it will be overwritten. As such, be careful when using the redirection symbol.
5.2. Redirect contents of multiple files to a single file
We have seen how to redirect the contents of a single file to another file. In addition, you can redirect or save the contents of one or more files to another single file as follows.
Here, we concatenate the contents of two files: myfile1.txt and myfile2.txt to the myfile3.txt file
cat myfile1.txt myfile2.txt > myfile3.txt
If you preview the file myfile3.txt , you will notice that it contains a combination of text from the first and the second text file.
cat myfile3.txt
You can make use of it in shell scripts to combine n number files for example combining a specific number of log files.
cat can also be used to concatenate files along with other commands using pipe. For example:
cat file1 file2 | grep foo
Files compressed by gzip can be directly concatenated into larger gzipped files:
cat file1.gz file2.gz file3.gz > combined.gz
6. Modifying Files
As mentioned in the introduction, cat command is primarily used to display or merge the contents of files. It is not typically used to modify the contents of files directly. However, we can use >> operator to append content to file(s). Let's look into a couple of examples.
6.1. Append content to a text file
We have seen that the redirection symbol overwrites the destination. Suppose you only want to add or append content to a file without overwriting it. How do you go about it?
To append content to another file, use the double greater-than >> operator.
In the example below, we have appended the contents of myfile2.txt to myfile4.txt.
cat myfile2.txt >> myfile4.txt
The second line represents the appended content from the myfile2.txt file.
6.2. Append / Add text to an existing file
Furthermore, you can append text to an already existing file without redirecting it from another file. To do so, simply type cat followed by >> and then the file name and press ENTER.
cat >> myfile4.txt
Next type the content you want to add and press Ctrl key + d to save and append the content to the file.
7. Using cat with Other Commands
Cat command can be combined with other commands such as more, less, sort or grep.
When viewing a large file, the Linux cat command typically takes you to the end of the file. This makes it difficult to view the file starting from the top. To comfortably scroll down screen by screen, then pipe the output to more as follows.
cat /etc/ssh/ssh_config | more
This allows you comfortably view a large file without a hassle.
The less command is similar to the more command, only that it is a bit advanced and lets you scroll page by page with the ability to move back and forth.
If you want to find a specific string in a file, you can use cat with grep.
cat filename.txt | grep "specific string"
If you want to display the contents of a file in a sorted manner, you can use cat with
cat filename.txt | sort
Note that in many cases, the cat command is unnecessary because commands like less, grep, sort, and more can operate on files directly.
8. Useful Options
There exist a few options in cat command. Let's explore some of the useful among it.
8.1. Numbering output of a file
Reading long files can be challenging especially if you are searching for a particular line. A better approach would be to number the output lines. The -n option allows you to display line numbers on the standard output in a numeric sequence.
cat -n myfile4.txt
8.2 . Display Non-Printing Characters
For displaying Non-Printing Characters with cat -v, cat -t, and cat -e.
To display the end of a file, use the -e flag using the following command.
cat -e myfile2.txt
The -e option instructs cat command to suffix the end of the line with a dollar sign ( $ )
You can easily display tab characters contained in lines using the -t flag. Here, we have created a file called myfile2.txt with two tab spaces.
The -t option instructs cat to indicate tab spaces by the ^I notation.
cat -t myfile5.txt
To display non-printing characters except for line endings and tab, use -v option.
cat -v filename1
It's also worth mentioning that these options can be combined. For example, you could use cat -vte to display tabs and line endings along with other non-printing characters.
8.3. Delete repeated output blank lines in a file
If you have a file with huge gaps between lines, you can get rid of repeated empty output lines using the -t option.
cat -s myfile5.txt
When comes to displaying and manipulating file contents, Linux is not limited to cat command. There are several other commands that can perform similar tasks such as less, more, tail, and head. For more advanced features, you can refer to bat - which modern replacement for cat command.
If this resource helped you, let us know your care by a Thanks Tweet.
Did you find this article helpful?
We are glad you liked the article. Share with your friends.
Comments