How to empty or clear the contents of an existing UNIX file

Contributor Icon Contributed by qmchenry Date Icon August 18, 2008  
Tag Icon Tagged: UNIX

If you want to empty the contents of a UNIX file, you could delete it and recreate it, but, as is typical of UNIX, there are more elegant alternatives. Here are a couple of ways to dump the innards of a file. If you know of any others, add them to the comments.


The special file /dev/null can and does contain nothing. While most often used as a sink of nothingness, for example writing output to it that you don’t want to keep, it can be used as a source of nothingness, too. To replace the contents of the file blah.txt with nothing, you can use the command

cat /dev/null > blah.txt

This command makes sense as it is using standard UNIX redirection to place the contents of one file (a known empty one) to another file. Another way to do this, although a less obvious way and probably less readable in a shell script by others, is

> blah.txt

Previous recipe | Next recipe |
 
  • MACMac
    The touch
    command may also be used to create a new file that is empty.
  • Anonymous
    Instead of emptying it you can simply fill it with nothing
    echo "" > file

    Cheers,
    Philip
  • Wrong! This inserts a newline. `echo -n >file` is correct. Use `ls -l file` to show the difference.
    These tips are important, as they do not close the file handle; useful for logs.
  • Anonymous
    Using echo you actually creates a file with 1byte (a single end of line character) and not a truly empty file as with the /dev/null method does.
    And just as a curiosity both methods also work on the OS X.
  • Anonymous
    Not sure you have read the question, but the real answer is to execute the following:

    cat /dev/null >/Filaname

    If the file is in use this will affectively zero out the contents, the only time I found this to be a problem is with Java programming. It appears the java program reads everything in to memroy and then writes to cache. If you hit the timing right, you can zero out the file, and all of a sound the full contents will be back. Like I said, I have only seen this in a java program writing to the disk. In all other cases, like zero'ing mail boxes, or large log files, it works welll.

    touch only creates an empty file if none are present, echo "" adds the unix delimiter LF.

    Later+
    Mike
  • MACMac
    You are correct about the touch command not zeroing out an existing file which was the point of the recipe.
  • Jesus E. Aneiros
    cp /dev/null blah.txt

    It will save you a t and a >.
  • Nice.
  • Jacob
    echo -n > file.txt
  • Aleq
    i was using grep a > bla.txt o_O
    it does empty the file, but you need to Ctrl+c after...
  • I use a lot of

    echo " " > blah.txt

    But whatever works will do :)
  • dinesh
    very helpful
  • aesha
    Thank you .This thing had worked out well for me
  • MNS
    Please follow the step to empty all the files. Here there can be some change of ";". It can be used or omitted according to the shell.

    for i in 'ls';
    do
    echo "" >$i;
    done
blog comments powered by Disqus