Information are one of the essential objects of any working system and Linux isn’t an exception. Information present a dependable approach of storing knowledge in a persistent method.
Linux makes use of plain textual content information to retailer essential configurations. For instance, the /and many others/hosts file shops static desk lookup for hostnames, the /and many others/crontab file comprises directions for the cron daemon, and so forth.
Definitely, we will use graphical instruments to create information. Nonetheless, the identical might be achieved utilizing the command line interface as effectively. On this easy-to-follow information, we’ll focus on varied methods of making a file in Linux.
1. Create an Empty File Utilizing > Redirection Operator
In Linux, the redirection operator (>)
is used to redirect the output of a command to a file as a substitute of displaying it on the terminal.
The identical (>)
operator can be used to create a file if it doesn’t exist already. Nonetheless, it makes the file empty if it exists already. Therefore one needs to be very cautious whereas utilizing the redirect operator.
$ > tecmint.txt $ head tecmint.txt
Within the above instance, we will see that the head command isn’t exhibiting any output because the file is empty.
2. Create File and Write Content material Utilizing > Redirection Operator
Generally, we need to create a non-empty file rapidly. In such instances, you need to use the output redirection operator (>)
to create a file and write content material to it utilizing the echo command as proven.
$ echo "Tecmint.com is a well-liked Linux weblog" > tecmint.txt $ head tecmint.txt
It is very important notice that on this instance, now we have used the echo command to create a file. Nonetheless, we will redirect the output of the different Linux instructions as effectively to create a file.
Additionally, it is very important notice that the >
redirection operator is used to overwrite the contents of an already present file, which trigger knowledge loss if the operation is carried out carelessly.
In such a case, we will use the >>
redirection operator, which is used to append the contents to the present file.
$ echo "Tecmint.com #1 Linux weblog" > tecmint.txt $ head tecmint.txt
Within the above output, we will see that the brand new line will get appended on the finish of the file.
It’s value noting that, similar to the redirection operator, the append operator may even create an empty file if it doesn’t exist already.
3. Create Information Utilizing contact Command
Yet one more approach of making a file is utilizing the contact command, which gives the most secure approach of making an empty file as a result of it by no means overwrites the present file. As an alternative, it simply updates the time stamp (entry time and modification time) of the present file.
$ contact tecmint.txt
4. Create Information Utilizing tee Command
Much like the redirection operator we will additionally use the tee command to create a file. The tee command writes the output of the command to the usual output stream in addition to the file.
For instance, to create a file named “tecmint.txt“, use the tee command, which shall be prepared to simply accept enter.
$ tee tecmint.txt
Now kind or paste the content material you need to write to the file after which press Enter and hit Ctrl + C
shut the enter window as proven.
If you wish to overwrite the content material of a file utilizing the tee command, you need to use the next command:
$ echo "Overwrite file utilizing the tee command" | tee tecmint.txt $ head tecmint.txt
On this instance, we will observe that the tee command overwrites the contents of the tecmint.txt file which was created and up to date in earlier examples.
To append the contents to the present file, use the -a
choice of the tee command, which permits us to append the information on the finish of the present file.
$ echo "Append knowledge utilizing the tee command" | tee -a tecmint.txt
5. Create a File Utilizing cat Command
We will use the mix of the cat command and redirection operator to create a file. For instance, the under command creates a brand new file if it doesn’t exist already.
$ cat > tecmint.txt
Right here, the terminal waits infinitely for the consumer enter. Now we have to press Ctrl + D
after coming into the required textual content to avoid wasting the contents to the file:
The principle benefit of this method is that it permits us to create a multi-line file utilizing an interactive approach. Identical to the redirection operator, now we have to make use of this methodology very fastidiously because it overwrites the present file.
In an identical approach, we will use the mix of the cat command and append operator to append the contents on the finish of the present file.
$ cat >> tecmint.txt
Identical to within the earlier instance, now we have to press Ctrl + D
to append the contents to the file after coming into the required textual content.
Conclusion
On this information, we mentioned easy methods to create a file utilizing the Linux command line interface. Linux novices can use one of many strategies to create a file from the terminal.
Are you aware of another approach of making a file from the terminal in Linux? Tell us your views within the feedback under.