The cat command is used in the Linux operating system. It is derived from the word concatenation which is used to concatenate the content of multiple files and write or print the content of any file. It is the most useful command in Linux. It doesn’t require any installation because it is a built-in utility that is provided by the Linux operating system. It provides various options to perform multiple actions on the files according to our needs.
The following is the syntax to use the cat command:
In the syntax, we can see that we pass two arguments along with the cat command. The first one is “option” which is an additional instruction that the user wants to perform like “-S”. It replaces many empty lines with a single empty line. Many other flags can also be used. The second argument that we pass is the name of the file that is to be displayed or created.
In this example, we first create new files. Then using the cat command, we do some operations on them like printing the content, concatenating, etc. Let us first create some files. To do that, we first create a new directory. It is not necessary to create a new directory to create the files. You can create the files anywhere in your system. To create a new directory, we run the “mkdir” command along with the file name as labeled in the following:
After running the provided command, we create a new directory named “cat_files” in our home directory. It won’t display any output. To ensure whether the directory is created or not, you have to check by running the command or by manually checking the home directory. When we don’t pass the path along with the command, it automatically creates the files in the home directory as shown in the following snippet:
Now, we dive into our “cat_files” directory using the “cd” command to create the text files inside the “cat_Files” directory.
When we run the previous command, our terminal looks like the following illustration which indicates that all performed actions are done inside the “cat_files” directory:
Let’s now create some files using the touch command:
It creates a new file named “myfile.txt” in the “cat_files” directory. We can add a content to it but this time, we want it blank and repeat this process. We create two more files named “myfile1.txt” and “myfile2.txt”. We can see that our files are created in the following snippet:
Now, using the cat command, we write some content in our “myfile.txt” file.
After running the previous command, it hands over the terminal to you without displaying any output to let you enter the content that you want to add to the file. You can add content to the file. In our case, we add the definition of the cat command as shown in the following snippet:
Now, you can manually check for the text file whether the content is added to the file or not. As shown in the following illustration, the content in the file is successfully added:
Now, we display the content of the file using the cat command in our terminal. For that, we run the following command:
To display the content of the file, we simply run the previous command as shown in the following screenshot. The content of the file that we added before is printed on our terminal:
Now, we copy the content of one file to the other file. Since we already added the content in the “myfile.txt” file, we now copy its content to another file that we created earlier named “myfile1.txt”. To do so, we run the following command:
The first file before the greater than sign “>>” is the file name from which the content is copied. While the filename after the greater than sign “>>” is the one in which the content is printed.
After running the previous command, we check for the “myfile1.txt”, whether the content is copied to it or not using the following command:
After running the command, the content of the file is printed on the terminal as shown in the following:
Same as this, we can also print the content of the file along with the number of lines which are written in the file by running the following command:
When we run the previously-mentioned command, it displays the content along with the number of lines that it has. The “-n” keyword is passed along with the file name to print the number of lines. The output is displayed like this in our terminal:
Let’s suppose we have some content in the third file named “myfile2.txt” and we want to append this file’s content to the other files. We use the following labeled command to perform this action:
By running the previous command, we append the content to “myfile1.txt” which is copied from the “myfile2.txt” file. One more thing to keep in mind is it does not replace the content that is already written in the file. It simply adds the newly copied content after that. Now, to check whether the file content is added or not, we run the cat command to print the content of the files on the terminal.
After running the previous command, we get the following displayed output in which the content is successfully added.
In this article, we discussed the cat command that is provided by Linux, its importance, and its uses. After introducing you to this command, we performed some operations on different files using the “cat” command in which we created new files and performed many other actions on them.