How To Create A Linux File Using The Command Line
There are a few easy methods to generate a text file via the Linux Command Line or Terminal. Some of them have been discussed in the following article.
1) touch command
This is the most typical command to rapidly create an empty text file. The command is pretty easy to write and makes it quite simpler to produce numerous text files at once. The commands are as follows:
touch filename.txt
As easy as that, simply put the term touch followed by the name of the file you desire to give it, and Voila! you have generated an empty text file inside of a terminal. You may put the file names of the file you would want to generate at once with space in between each filename. The program below produces three empty files at once using the touch command and you may create as many files as you want.
2) Standard Redirect Symbol(>)
It is also fairly simple to grasp the command to create a text file in the terminal with minimal effort. This works really really well for making a single text file rapidly, however, creating numerous text files at once gets a little tiresome. The command is just utilizing the normal redirect sign (>) spacebar followed by the file name.
> filename.txt
If you wish to generate numerous text files at once, then you may put the redirect symbol after the previous filename and chain the command repeatedly to create several empty files.
> file.txt > file2.txt > file3.txt
The above program produces three empty text files. The redirect sign is highly time-saving if you only want to produce a single text file. It becomes rather longer than the touch command to generate many empty text files.
3) CAT Command
Now, this approach is also fairly basic and straightforward to utilize. Simply type in CAT with two redirect symbols (>>) and the file name( It is not required to use >> symbols, a user may also use > symbol, but if the user types a pre-existing file by accident, the existing content in the text file will be rewritten using a single > symbol). This approach is a type of mix of touch and the reroute symbol instructions. This approach is a little odd, so you only prefer using the above two instructions if you want to create an empty never altered file. If you want to create and write in the text file immediately away, by far this is quite a wonderful solution. This will save you time to open up an editor and the command is also pretty straightforward.
The below command generates an empty but modified file as it invites the user to create a text file and type in the file at the same time. So, if you do not wish to change the file, just click CTRL+C and it will simply leave and generate an empty file.
cat >> file.txt
But, if you would want to add some content to the file, you may write in after this, like this:
cat >> new.txt
This is some content in the file from the command line.
To end editing and saving in the file, just enter CTRL+C, it will create, save and leave the file. So, this way is quite a time-saving method if you want to change the text file really rapidly. The following command will add the text to the pre-existing file. On the other hand, if you use a single redirect symbol(>) it would overwrite the content of the file, therefore you only prefer using two redirect symbols for safety reasons.
4) Using echo / printf
This is similarly similar to the cat command, although it is quite versatile relatively. The following command is commonly used for printing text on the terminal, but we can also use it to write to a file or construct an empty file. The echo command is used together with the double redirect symbols (single > will also work) followed by the filename.
echo >> filename.txt
If you wish to create numerous files at a time, you may chain up the command as in earlier approaches.
echo >> file1.txt >> file2.txt >> file3.txt
We can also add the capability to the echo command to fast create and write to the text file similar to the cat command.
echo -e ‘This will be the content in the file \n this is the new line’ >> file.txt
The above command may be quite customized since it exploits the capabilities of the echo command to make it fairly adaptable to put the text in the file, however using a new line character every time can be unpleasant as well.
Similar to the echo command, we have the printf command as well. The print command accomplishes 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 accomplishes some very C-like things, such as the newline character and the variable names may be used as well, however that is not for a plain text file. Nevertheless, the printf command may be beneficial in a lot of scenarios to modify files on the move.
5) Any command-line text editor(Vim, nano)
This is the most time-consuming way and not the quickest, although the method may be beneficial for Linux newcomers. If you wish to significantly modify a text file, you may use command-line text-editors such as Vim, nano, and there are other possibilities as well. But most people prefer nano since it is easy to use and fast to go. Vim may also be used but most novices find it difficult to use, so we’ll stay with nano for this example.
nano filename.txt
vim filename.txt
We are currently in the nano editor(or vim). You may enter in the items you need and just press CTRL+S to save and CTRL+X to quit. In Vim it is a little different.
So that sums up the ways for fast generating a text file or writing to the file. Each strategy may be employed differently based on the scenario and the case chosen. Not every approach will be the quickest, although these were some of the fastest methods to create a Text File Using the Command Line in Linux.