The cat or concatenate command is a versatile utility for combining two or more files. You can also use the cat command to print a file’s content on the terminal without opening it in a text editor.
The cat command has various other functionalities like appending to files, displaying the number of lines, creating new files, etc. However, many users, especially beginners, know little about the cat command. So this article has everything a novice needs to know about the cat command in Linux.
How To Use Cat Command in Linux
As we have mentioned earlier, the cat command is used to display a file’s content. So here is the basic expression of the cat command:
Now let’s take an example to display the content of the script.txt file:
If you have run a script or a piece of code and want to save its output to a file, please run the following command:
If any other file named ‘output.txt’ does not exist in your current directory, this command will first create it. Then, it will save the output of the previously executed command into it. To concatenate multiple files and then display their content, use the following command:
Along with the file contents, you can show their line numbers using the -n option:
You can use the below command when you have two files, i.e., file1 and file2, but want to append the content of file2 to file1:
Similarly, you can clone the content of one file to another:
A Quick Recap
The cat command is a powerful and versatile utility offering multiple features. However, users often are unaware of its true potential, which leaves it underutilized. Therefore, this article briefly explains the cat command, its options, and various use cases.
We have demonstrated how to use the cat command to display a file’s content, make a file’s copy, append it to another file, and show the number of lines, etc.