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

Contributor Icon Contributed by qmchenry  
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

 

21 Comments -


  1. MACMac said on October 2, 2008

    You are correct about the touch command not zeroing out an existing file which was the point of the recipe.

  2. Jesus E. Aneiros said on October 8, 2008

    cp /dev/null blah.txt

    It will save you a t and a >.

  3. Jacob said on October 15, 2008

    echo -n > file.txt

  4. Quinn McHenry said on October 15, 2008

    Nice.

  5. Aleq said on November 20, 2008

    i was using grep a > bla.txt o_O
    it does empty the file, but you need to Ctrl+c after…

  6. Tech Blog said on April 18, 2009

    I use a lot of

    echo ” ” > blah.txt

    But whatever works will do :)

  7. dinesh said on May 15, 2009

    very helpful

  8. Bruce said on May 21, 2009

    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.

  9. Anonymous said on August 29, 2009

    Thank you .This thing had worked out well for me

  10. MNS said on October 9, 2009

    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

  11. Anonymous said on February 16, 2010

    If there is a file “abc.txt” in your unix platform
    use :> for deleting contents of a file “:> abc.txt”

  12. Anonymous said on February 17, 2010

    Important to note- the ‘> filename’ method will only work on sh, and ksh variants but not csh.

    –Dennis

  13. Anonymous said on February 26, 2010

    Nothing just cat the file with echo ” ” as mentioned below to empty the file.

    Include this command in the script will get all the details in it washed away

    echo ” ” | cat >

    Ex : echo ” ” | cat > temp

  14. DINESH said on May 24, 2010

    echo -n > blah.txt

  15. Marty69 said on August 10, 2010

    I’m a newbee at this so i assume if u pip this into the temp folder u will get the same results?

  16. Guest said on August 13, 2010

    : > blah.txt

  17. Geo said on November 3, 2010

    # touch test
    # cat test > blah.txt

    this commands will empty the file blah.txt

  18. Vinothkumar Sundaram said on November 9, 2010

    Sorry it is working

  19. Duncan Anderson said on December 13, 2010

    :> filename

    Has always been my choice, especially in shell scripts.

  20. Sandeep D. said on April 18, 2011

    Thansk. This is the best way to clear the contents of a file
    UNIX PROMPT> :> [file name]

  21. Lakshmanan Samy said on November 12, 2011

    Thanks these are all helpful

 

RSS feed for comments on this post. TrackBack URL

Leave a comment -