How to delete or empty the content of file in Linux

In this article, we will see 8 ways to delete or empty the content of file in Linux.

In Linux terminal, we have to delete or empty the content of file(s) from terminal or in shell scripts. We can achieve this by various methods. We will delete only the data of file but not the file.

Caution:

Once delete a file cannot be restored. Make sure you are deleting right file. Delete wrong user file or system file cause serious damage to your environment. If you have proper permissions then only delete files.

Delete or empty the content of file in Linux examples:

1. Delete or empty file content using by redirecting file to null.

> backups.txt

2. Delete or empty file content using cat command.

cat > backups.txt

press enter and ctrl+d

3. Delete or empty file content using true command.

true > backups.txt

4. Delete or empty file content using /dev/null.

cat /dev/null > backups.txt

5. Delete or empty file content using echo command. It deletes data but size is not zero.

echo > backups.txt

6. Delete or empty file content using truncate command.

true -s 0 backups.txt

7. Delete or remove contents of file in Linux using sed command.

sed -i d backups.txt

8. Delete file content in Linux using rm and touch commands together.

rm backups.txt; touch backups.txt;

How to delete or empty the content of file in Linux

So, in this we have seen 7 different ways empty or delete the file content in Linux with examples.