How to clear a file in Linux
Colleagues hello to all.
In today's article, I will tell you about different ways to clean up a file. Sometimes we all have to work with files in the Linux terminal and suddenly we need to clear the contents of a file without opening the file itself using any command line editors lines.
Article content:
- Redirect operator.
- cat.
- The echo command
- cp command
- Truncate command.
- Summary
1. Redirect operator.
To clean the contents of a file, we can simply use the redirect operator (> ) before the file name.
$. > file1.sql
2. The cat command.
Alternatively, you can clear the contents of a file by redirecting the output of /dev/null to a file as input with the cat command.
$. cat /dev/null > file1.sql
3. The echo command.
The echo command is used to output the contents to the console, thereby redirecting an empty string to a file.
$. echo """ > file2.sql
4. cp command.
The cp command is actually used to copy something, but it can also clear the file.
$. cp /dev/null file3.sql
5. truncate command.
You can use the truncate command to reduce or increase the size of a file.
$. truncate -s 0 file4.sql
6. Results.
In today's article, we looked at how to clean up files in Linux. In fact, there are still a lot of ways, but I showed you the most frequent that everyone uses.
Thank you all, I hope that my article helped you in some way.