The βcatβ command is a versatile Linux command that you can use for various purposes which varies from creating and merging files to writing a text to a file. It is one of the simplest commands which you must know as a beginner.
However, many Linux users don’t know about the feature of the βcatβ command which writes a text to a file. In this quick blog, we will explain the various examples on how to use the βcatβ command to write a text to a file in Linux.
Cat Command to Write a Text to a File
Let’s start with creating a βmy_file.txtβ text file using the βcatβ command:
After running the previous command, you can add any line accordingly. For example, let’s add the following line:
You can add more lines below the line. Once youβre done adding the text that you wish to add to your file, press βCtrl+Cβ to end the command.
The Difference Between β>β and β>>β
While you can use both β>β and β>>β while working with the βcatβ command, thereβs a difference between the two that you need to understand.
While using β>β, you must ensure that the name you use for the file is unique. That’s because if a file with the supplied name is already available, the β>β symbol will overwrite the existing content in that file.
Conclusion
The βcatβ command has a range of applications and can be used for various tasks including adding a text to the files directly from the Linux terminal. The provided explanation will help you to work better with files using the Linux βcatβ command. We recommend you to press the βCTRLβ and βCβ keys. Otherwise, you cannot exit the βcatβ command.