Open In App

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

Last Updated : 31 Jul, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

There are a couple of quick ways to create a text file from the Linux Command Line or Terminal. Some of them have been explained in the following article.

1) touch command

This is the most standard command to quickly create an empty text file. The command is quite simple to type and makes it quite easier to create several text files at once. The commands are as follows:

touch filename.txt

As simple as that, just type the word touch followed by the name of the file you like to give it, and Voila! you have created an empty text file inside of a terminal. You can add the file names of the file you would like to create at once with space in between each filename. The command below creates three empty files at once using the touch command and you can create as many files as you like.

touch file1.txt file2.txt file3.txt 

touch command

touch command for creating multiple files

2) Standard Redirect Symbol (>)

Creating a text file in the terminal is a straightforward process that requires minimal effort. It works efficiently when creating a single text file promptly. However, when the need arises to create multiple text files simultaneously, the process can become tedious. The command involves using the standard redirect symbol (>) followed by a spacebar and the desired file name.

> filename.txt 

If you want to create several text files at once, then you can add the redirect symbol after the previous filename and chain the command repeatedly to create multiple empty files.

> file.txt > file2.txt > file3.txt

The above command creates three empty text files. The redirect symbol is quite time-saving if you just want to create a single text file. It gets quite longer than the touch command to create multiple empty text files.

Using the redirect symbol for creating files.

3) CAT Command

This method is incredibly simple and user-friendly. To create a new text file using this method, all you need to do is type “CAT” followed by two redirect symbols (>>) and the desired file name. While it is not necessary to use the >> symbols, you can also use a single > symbol. However, caution must be exercised when using a single > symbol, as it can inadvertently overwrite the existing content in the text file if the file already exists. This method combines the functionality of the touch and redirect symbol commands. It is a unique approach, best suited for creating empty, never-edited files. If you prefer to create and type directly into the text file, this method is highly efficient. It eliminates the need to open a separate editor, saving you time and offering a straightforward command.

The below command creates an empty yet edited file as it prompts the user to create a text file and type in the file at the same time. So, if you do not want to edit the file, simply press CTRL+C and it will simply exit and create an empty file.

cat >> file.txt

But, if you would like to add some text to the file, you can type in after this, like this:

cat >> new.txt
This is some text in the file from command line.

To stop editing and saving in the file, simply type CTRL+C, it will create, save and exit the file. So, this method is quite a time-saving method if you want to edit the text file very quickly. The following command will append the text to the pre-existing file. On the other hand, if you use a single redirect symbol(>) it will overwrite the content of the file, so you only prefer using double redirect symbols for safety reasons.

Using cat command to create the file.

Using cat command to create and write a file.

4) Using echo / printf

The echo command, similar to the cat command but more versatile, is commonly employed to display text on the terminal. However, its functionality extends beyond that, as it can also be used to write content to a file or create an empty file. To accomplish this, the echo command is utilized in conjunction with double redirect symbols (single “>” can also be used) followed by the desired filename.

echo >> filename.txt

If you want to create multiple files at a time, you can chain up the command as in previous methods.

echo >> file1.txt >> file2.txt >> file3.txt

We can also add functionality to the echo command to quickly create and write to the text file just like  cat command.

echo -e ‘This will be the text in the file \n this is the new line’ >> file.txt 

The above command can be highly customizable as it uses the properties of the echo command to make it quite versatile to write the text in the file, but using a new line character every time can be annoying as well. 

using the echo command to create files.

Using echo command to create and write to a file.

Similar to the echo command, we have the printf command as well. The print command does the same thing as the echo command but in a C style rather than shell-style editing.

printf "" >> filename.txt
printf "" >> file1.txt >> file2.txt >> file3.txt

printf “This is some text here \n The second line \n The third line” >> file.txt

The print command does some pretty C-like things, such as the newline character and the variable names can be used as well, but that is not for a simple text file. But still, the printf command can be useful in a lot of cases to edit files on the go.

Using printf command to create files.

Using printf to create and write to files.

 

5) Any command-line text editor(Vim, nano)

This is the most time-consuming method and not the fastest, yet the method can be useful for Linux beginners. If you want to heavily edit a text file, you can use command-line text-editors such as Vim, nano, and there are other options as well. But most people use nano as it is simple to use and quick to go. Vim can also be used but most beginners find it difficult to use, so we’ll stick with nano for this example.

nano filename.txt
vim filename.txt

We are now in the nano editor(or vim). You can type in the stuff you require and simply type CTRL+S to save and CTRL+X to exit. In Vim it is a bit different. We won’t make a vim guide here, so you can check out the ‘Nano text editor in Linux‘ or ‘Getting started with vim‘ article from geeks for geeks.

Using Nano to create and write files.

So that wraps up the methods for quickly creating a text file or writing to the file. Each method can be used differently depending on the situation and the case used. Not every method will be the fastest, yet these were some of the fastest ways to create a Text File Using the Command Line in Linux.

Conclusion

In this article, we discussed several quick and efficient methods for creating and manipulating text files from the Linux command line. These methods, including the touch command, redirect symbol, CAT command, echo/printf commands, and command-line text editors like Vim and nano, offer different approaches to meet various needs. Whether it’s creating empty files, appending text, or extensively editing content, users can choose the method that best suits their requirements. Overall, these methods empower Linux users with the ability to conveniently and swiftly handle text files directly from the command line.



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads