The cat command is one of the commonly used Linux commands, and its main purpose is to display the contents of an existing file. However, there are other numerous ways of using the Linux cat command when working with files, including creating new files, concatenating files, appending a file’s contents, etc.
The cat command blends with numerous Linux commands. If you are new to Linux, understanding the cat command is essential. This tutorial dives into understanding the cat command in Linux. While at it, we will give practical examples of how to use the cat command when working with files. Let’s begin!
Understanding the cat Command
The ‘cat’ command stands for ‘concatenate’ and it comes pre-installed in Linux distros. The command is majorly used to display the contents of an existing file(s) but has other uses depending on your goal.
Here’s the syntax for the cat command.
$ cat [OPTION] [FILE]
The Linux cat command accepts various options to get specific results. Moreover, it can be used with one or more files, as seen in the example section.
Practical cat Command Examples
Let’s discuss a few examples to clarify how to use the command.
1. Creating a File Using the cat Command
The cat command can be used to create new files. Unlike other commands, such as the Linux touch command that also creates files, the cat command allows you to create a file and add contents.
Follow the syntax below.
$ cat > file
We’ve created the ‘hello.txt’ that we will use throughout this tutorial and added contents to it. Press the Ctrl + d option to save the changes.
We can verify that we used the command to create a new file by running the ls command to list the available files.
2. Reading/Displaying Contents of an Existing File
Suppose you have a file and want to display its contents. You can run the cat command and add the file. Its contents will be displayed in the standard output.
$ cat file
You can add line numbers for every contained line in your file by adding the -n option. Note that even blank lines will be included in your output unless you specify to skip them.
$ cat -n file
3. cat Command Append to File
With the cat Linux command, you can quickly append the contents of a file. That way, you don’t have to open the file using a text editor. To append text to a file, use the “>>” symbol with the cat command.
$ cat >> file
The text you type in the terminal will get added to your existing file after you press the Ctrl + d to save the changes.
Run the command to view the contents of the appended file. It will contain the text you’ve added and will appear at the bottom of the file.
Additionally, you can use the Linux cat command to append the contents of a file to another file.
Use the syntax below.
$ cat existing_file >> appended_file
For our case, we have the names.txt, and we’ve appended its contents to the file3.txt.
4. Remove Empty Lines When Using the cat Command
The cat command accounts for every line, including blanks. However, if you don’t want to capture the blank lines in your output, you can add the -b option.
The example below demonstrates what displaying the contents of a file will appear when we add and omit the -b option. The first instance shows that all lines are included. However, the second instance skips the blank lines.
5. Copy File Contents
How about creating a copy of a file by copying its contents to another file? Besides, the file you wish to copy to will get created when you run the command if it doesn’t exist.
$ cat original_file > new_file
We’ve displayed the file’s contents to confirm that it’s a replica of our original file, meaning we’ve managed to copy the contents of our ‘hello.txt’ to the newly created ‘copied.txt.’
6. cat Command Concatenate Multiple Files
We’ve already discussed how you can copy the contents of one file to another. Still, it’s possible to concatenate the contents of multiple files into one file. The example below concatenates two files using the “>” symbol.
$ cat file1 file2 file3 file_n > output_file
The output file doesn’t have to exist, as it will get created with the command. We’ve run the cat command on it and verified that it contains the concatenated text from the files.
7. Display Contents of Multiple Files
Another handy use of the cat command is viewing the contents of multiple files in one output. Instead of passing one file, you can include multiple files. The cat command will display the output of all the included files.
Here’s an example.
8. Show the End of the Line
The -e option is used with the cat Linux command to add the dollar sign to indicate the end of a line.
$ cat -en file
It will also include the blank lines unless you specify to skip/remove them.
9. cat Command Reverse Output
The tac command reverses the order of the displayed output when running the cat command. It will display the contents of a file starting with the bottom line.
$ tac file
The example below shows the difference in the output when we run the cat and the tac command on a file. Notice how the displayed contents are arranged.
10. Using ‘more’ and ‘less’
Sometimes, you may have a case where the target file you wish to display its contents using the cat command contains tons of data. As such, the data can’t be displayed on a single page.
This instance requires you to combine the cat command with the ‘more’ and ‘less’ commands.
The ‘more’ command will split the output into pages. It follows the syntax below.
$ cat [options] file | more
In contrast, the ‘less’ command makes the output scrollable, forward and backwards, for better display. Use the syntax below.
$ cat [options] file | less
11. View Help Page
The cat Linux command has a help page that shows all the options you can use for specific results. To access this help page, use the below command.
$ cat --help
Conclusion
You can’t be a Linux/UNIX user without understanding how to use the cat command. Fortunately, this post has discussed the command in detail, giving practical examples of how to use it. With this knowledge, you are one step closer to becoming a pro-Linux user!