How to Empty File Content in Linux

Deleting a file in Unix/Linux is as simple as issuing the rm command. But in this tutorial, we show some methods that will empty (truncate) a file in Linux rather than deleting it. It means that the contents of the file will be removed and not the file itself.

1) Truncate Command

Use truncate with -s 0 option sets the file size to zero.

For example, below command will set 'kern.log' file size to zero

sudo truncate -s 0 /var/log/kern.log
$ sudo du -sh kern.log
48K     kern.log
$ sudo truncate -s 0 /var/log/kern.log
$ sudo du -sh kern.log
0       kern.log
$

You can do the same for multiple files as below

$ sudo truncate -s 0 /var/log/**/*.log

2) Using ‘cp’ (copy) command

‘/dev/null’ is a special file that contains nothing. Whatever you put in this file, it disappears forever. It is like a black hole, whatever goes in never comes out. This command copies ‘/dev/null’ to the file to be emptied.

$ cp /dev/null raghu.txt

3) Using ‘dd’ command

The dd command copies raw files in the form of bytes. Two files are supplied to the ‘dd’ command, an input file, given by if=/dev/null and an output file, of=raghu.txt. The bs=1 value represents the block size (this cannot be 0) and count=0 is the number count (0 here) of blocks, i.e. how many blocks (of size given by ‘bs’) must be copied.

$ dd if=/dev/null of=raghu.txt bs=1 count=0

4) Redirection method

The following commands use redirection for removing the contents of the file.

$ > raghu.txt

5) Using echo command

You can use below echo command to empty file contents

$ echo -n "" >raghu.txt

6) Using cat dev null

The following cat command puts the contents of the file ‘/dev/null’ in raghu.txt

$ cat /dev/null >raghu.txt

or

$ cat > raghu.txt

And then press ctrl + d.

In this method, the cat command puts everything you type afterward in the file 'raghu.txt', until ‘ctrl+d’ id pressed. So here, we do not write anything to file and directly press ‘ctrl+d’ so that "nothing" goes in the file.

You can also use other tools like ‘sed’ or ‘awk’ with redirection to remove file contents.

7) Using vi editor

And finally, using a text editor like ‘vi’. This is the most obvious method

Open the file in ‘vi’ editor (or any other editor).

$ vi raghu.txt

And then press ‘99999dd’. This will delete at most 99999 lines from the file. If you believe that your file can have more than 99999 lines, you can precede “dd” with a number greater than the number of lines in your file. Now save the file and quit from the editor using “:wq”.

If you are using some other advanced editor like “gedit”, then you can press ‘ctrl+a’ to select all the text in the file and then press backspace or delete key to remove the text. Now, save the file and exit from your editor.

Conclusion

In this tutorial, we learned different ways to empty file content in Linux. I hope you enjoyed reading and please leave your suggestions in the below comment section.

Raghu 9:24 pm

Comments

Your email address will not be published. Required fields are marked *

All comments are subject to moderation.

2 Comments

  1. A more reliable way to delete from the cursor location to the end of the file in vi is to simply type dG

    d for delete mode and G to jump to the end of the file.