Following are some basic functions of cat command:
- Displaying contents of the file
- Creating new files
- Combining files
- Copying file contents
Note that we have used Debian 10 OS for explaining the commands and procedures mentioned in this article.
Basic Syntax
The basic syntax for using the Cat command is:
Use the above command if you are in the same directory that contains your file. Otherwise, if you are in some other directory, mention the path to that file as follows:
Display Contents of File
The most common and basic usage of cat command is displaying the file contents. In order to display the file contents to a Terminal, simply type cat and the filename as follows:
An example of this would be to display the contents of /etc/hosts file. In that case, the command would be:
Display content of All Files
To display all the files in a current directory, use the wildcard character with the cat command as follows:
To display only the contents of text files in a directory, enter the following command:
Display Multiple Files at Once
You can also combine and display the contents of multiple files together in the Terminal using the cat command. To display multiple files simultaneously, use the following syntax:
An example of this would be to view the output of all the three files /etc/hostname, /etc/resolv.conf and /etc/hosts in the Terminal as shown in the following screenshot:
Copy the output of one file to another file
It can also be utilized to copy the output of one file to another file. If the destination file does not exist, it will first create it, otherwise overwrites the targeted file.
To copy the output of a source file to another file, use the following syntax:
An example of this would be to copy the output of a testfile1 to another file named testfile_backup as follows:
This command will first create the file testfile_backup and then copy the contents of testfile1 to it.
Append the output of a file to another file
Instead of overwriting the output of a targeted file in the above example, you can also make the cat command to append the output. The following syntax can be used for this purpose:
It will create the destination file if it does not already exist, otherwise will append the output.
Copy multiple files to another text file/ Concatenating the files
Another major use of cat command is that you can combine multiple files into a single file. Following syntax can be used to concatenate the file1, file2, and file3 and save them to another file named file4.txt.
For instance, we want to concatenate the output of /etc/hostname, /etc/resolv.conf and the /etc/hosts file to another file named network.txt. The following command can be used for this purpose:
Display Line Numbers in File
To display line numbers to the output of a file, simply use –n flag s follows:
For instance, if you are viewing a file containing the list of items, then you can use the –n flag to display those items with a number. Remember that empty lines are also numbered as shown in the following screenshot:
If you do not want to number the empty lines, use –b flag as follows:
Create a File
You can also create a file using the cat command. The following syntax can be used for thi spurpose:
After entering the above command, enter the text you want to store in the file. Once done, use Ctrl+D to save and exit. After that, you can view the contents of your newly created file by executing the following command in Terminal:
Sorting output
You can also combine the sort with the cat command to sort the output alphabetically as follows:
Similarly, in case of multiple files, you can concatenate the output into one file in an alphabetical order:
Remove consecutive empty lines
Sometimes the file contains consecutive empty lines that you do not want to print. Cat command allows merging those consecutive empty lines and shows them as one empty line.
Use the following command syntax to remove the repeated empty lines:
For instance, we have the following file with consecutive empty lines.
By using the –s flag, you will see the difference.
Display tab characters
Sometimes, you have to remove tabs from your files. Cat command can help you to find the tabs on your file by using the –t flag as follows:
Tabs will be shown in the output as ^I characters.
Printing output of a file
Another popular use of cat command is in the printing contents of a document. For instance, to print the output of a file to a printing device named /dev/lp, the following syntax will be used:
In this article, we have explained through various examples how you can use the cat command to manipulate the files in Linux. Cat command is popular among all users because of its simple syntax and the lot of options that it provides. Creating and viewing a file, merging, copying, and appending the file contents, printing, and a lot more can be handled with this single cat command.