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

17 Jun, 2021

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.
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
It is also quite easy to understand the command to create a text file in the terminal with the minimum effort. This works really very well for creating a single text file quickly, but for creating several text files at once it becomes a bit tedious. The command is simply using the standard redirect symbol (>) spacebar followed by the 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.
Now, this method is also quite simple and easy to use. Simply type in CAT with two redirect symbols (>>) and the file name( It is not mandatory to use >> symbols, a user can also use > symbol, but if the user types  a pre-existing file by mistake, the existing content in the text file will be overwritten using a single > symbol). This method is a kind of combination of touch and the redirect symbol commands. This method is a bit quirky, so you only prefer using the above two commands if you want to create an empty never edited file. If you want to create and type in the text file straight away, by far this is quite a brilliant method. This will save you time to open up an editor and the command is also quite easy. 
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.
This is also similar to cat command, but it is very flexible comparatively. The following command is usually used for printing text on the terminal, but we can also use it to write to a file or make an empty file. The echo command is used along with the double redirect symbols (single > will also work) followed by the 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.
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.

Linux-file-commands


Picked


Linux-Unix

Writing code in comment? 
                                    Please use ide.geeksforgeeks.org, 
                                    generate link and share the link here.
                                
Thread functions in C/C++
Array Basics in Shell Scripting | Set 1
scp command in Linux with Examples
chown command in Linux with Examples
nohup Command in Linux with Examples
Named Pipe or FIFO with example C program
mv command in Linux with examples
SED command in Linux | Set 2
Basic Operators in Shell Scripting
Start/Stop/Restart Services Using Systemctl in Linux