How to clear a file in Linux

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:

  1. Redirect operator.
  2. cat.
  3. The echo command
  4. cp command
  5. Truncate command.
  6. Summary

 

1. Redirect operator.

To clean the contents of a file, we can simply use the redirect operator (> ) before the file name.

$. > file1.sql

linux_clear_file


 

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

linux_clear_file


 

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

linux_clear_file


 

4. cp command.

The cp command is actually used to copy something, but it can also clear the file.

$. cp /dev/null file3.sql

linux_clear_file


 

5. truncate command.

You can use the truncate command to reduce or increase the size of a file.

$. truncate -s 0 file4.sql

linux_clear_file


 

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.

SIMILAR ARTICLES

mv command in linux. Moving/Renaming files.
mv command in linux. Moving/Renaming files.
TERMINAL
Read more
tail command in linux. View the last lines of a file.
tail command in linux. View the last lines of a file.
TERMINAL
Read more
Linux command cal, calendar
Linux command cal, calendar
TERMINAL
Read more