How to delete content of a file without deleting a file

How to delete content of a file without deleting a file

by -
0 1081

Deleting a file using rm command and re-create  using same name is very simple, but we should remember permission,attribute,ownership of a file before recreate the file. But here we present some methods that will empty your file rather than deleting it. It means that the contents of the file will be removed and not the file itself.

Here we are using a file data.log  as a example. Many method are given here o blank the content of  file data.log without deleting this file. Very very useful commands, when we delete the log files.


Using ‘dd’ command

# ls –l  data.log

-rw——-. 1   root    root    634442    Jun 27 18:20   data.log

# dd   if=/dev/null   of=data.log   bs=1   count=0


0+0 records in

0+0 records out

0 bytes (0 B) copied, 0.000294764 s, 0.0 kB/s

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= data.log.  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.

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

# ls -l data.log

-rw——-. 1  root  root  0  Jun 27 18:23 data.log


Using ‘cp’ (copy) command

#  cp /dev/null data.log

‘/dev/null’ is a special file that contains nothing. Whatever you put in this file 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.

# ls -l data.log

-rw——-. 1  root  root  0  Jun 27 18:23 data.log


Using editor like ‘vi’  ( Very Simple )

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

$ vi data.log

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.


This is another method using cat command

# cat >  data.log

And then press ctrl + d.

In this method, the cat command puts everything you type afterwards in the file data.log, 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.


# cat /dev/null > data.log

This is another method using  echo command

# echo  > data.log

This will remove all content of a file , but not able to zeroed the file. One blank space occupied 1K space.


echo -n “” > data.log


This is amazing the simplest one

# > data.log


Enjoy Linux ….it works.

Download PDF

CEO, KV IT-Solutions Pvt. Ltd. | [email protected] | 9810028374|
Linux Professional and an Industrial Trainer | 20 + years Experience in IT Industry

” We are born free, No Gate and Windows can snatch our freedom “


0 1700

0 1354

0 1218


Leave a Reply