How to Quickly Create a Text File Using the Command Line in Linux

If you are a keyboard person, you can accomplish a lot of things just by using the Linus command line. For instance, there are some easy-to-use methods you can use to create text files, should you need to do so.

Create a Text File Using the Cat Command

The first method we will go through which you can use to create text files uses the cat command. This method is pretty useful in cases where you want to immediately add some text file to your new file.

To use this method, simply type the following command at the terminal prompt (replacing “sample.txt” with whatever you want the name of your file to be), and then press Enter:

cat > sample.txt

Once you press Enter, you are not returned to the terminal prompt. Instead, the cursor will be moved to the next line, and there you can begin typing text directly into your file. Enter your lines of text, and after each line, press Enter, When you are done, press Ctrl+D to exit the file and go back to the prompt.

To verify your file was created, you can easily use the ls command to view a directory listing for the file:

ls -l sample.txt

You can also use the cat command to show the contents of your file. Simply enter the following command at the prompt and when you are done, press Enter.

cat sample.txt

Create a Text File Using the Touch Command

You can also easily create a text file using the touch command in Linux. The main difference between using this command and the cat command we just covered above is that, while the cat command allows you to type text into your file immediately, the method involving touch command does not. Another main difference between them is that the touch command allows you to create multiple files with a single command but you cannot do that with the cat command.

The touch command is useful for quickly creating files which you plan on using later.
To create a new file, simply enter the following command at the terminal point and replace “sample.txt” with whatever name you wish to use for the file. When you are done, press Enter:

touch sample.txt

Notice that there is no indication given to you when the file was created; you are only taken back to the prompt. You can verify the existence of your new file using the ls command:

ls -l sample.txt

You can also use the touch command to create multiple new files at a time.Just add as many extra filenames (separated by spaces) as you want to the end of the command:
touch sample1.txt sample2.txt sample3.txt

Again, you will not be shown any indication that the file was created. However, you can use the ls command to show that the files have indeed been created.

And when you are ready to add text to your new files, you can use a text editor like Vi to do that.

Create a Text File Using the Standard Redirect Symbol (>)

You can also use the standard redirect symbol to create a text file, which is usually used to redirect the output of a command to a new file. If you use the standard redirect symbol without any a preceding command, the redirect symbol just creates a new file.

Just like the touch command, when you create a text file this way, it will not let you enter text into the file right away. Unlike the touch command, however, you can only create one file at a time using the Standard redirect symbol. We are including this method for completeness and also because if you are just creating a single file, it does offer the least typing.

To use the Standard redirect symbol to create a file, type the following command at the terminal prompt and replace “sample.txt” with the name you want for the file. When you are done, press Enter.

sample.txt

You will not be given any indication that the file has been created, but you can use the ls command to verify the existence of the file:

ls -l sample.txt

You might want to check out these posts as well:

Don't Tell Anyone! Enter Your Email Address To Receive Free Updates!!!

LEAVE A REPLY

Please enter your comment!
Please enter your name here