Linux Commands

How to Use the Cat Command to Write a Text to a File

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:

cat >> my_file.txt

 
After running the previous command, you can add any line accordingly. For example, let’s add the following line:

This is my file

 

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.

About the author

Prateek Jangid

A passionate Linux user for personal and professional reasons, always exploring what is new in the world of Linux and sharing with my readers.