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

Posted August 18, 2008 by Quinn McHenry in 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


About Quinn McHenry

Quinn was one of the original co-founders of Tech-Recipes. He is currently crafting iOS applications as a senior developer at Small Planet Digital in Brooklyn, New York.
View more articles by Quinn McHenry

The Conversation

Follow the reactions below and share your own thoughts.

  • 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 >.

    • Quinn McHenry


  • Jacob

    echo -n > file.txt

    • Shawn

      Best one!

    • Haider Abbas

      This really helped me. Thanks!

  • Aleq

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

    • Dinesh

      I have files like test1 test2 test3 test4 test5 in an directory, how to empty in sigle command, am not able do so with # >test*. Pls help.

  • Tech Blog

    I use a lot of

    echo ” ” > blah.txt

    But whatever works will do 🙂

  • dinesh

    very helpful

  • Bruce

    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

    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’;
    echo “” >$i;

  • Anonymous

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

    • Sandeep D.

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

  • Anonymous

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


  • Anonymous

    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


    echo -n > blah.txt

  • Marty69

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

  • Guest

    : > blah.txt

  • Geo

    # touch test
    # cat test > blah.txt

    this commands will empty the file blah.txt

  • Vinothkumar Sundaram

    Sorry it is working

  • Duncan Anderson

    :> filename

    Has always been my choice, especially in shell scripts.

  • Lakshmanan Samy

    Thanks these are all helpful

  • AP

    thank-you !!

  • Santu

    very useful..

  • rajiv

    I want to keep a few last lines in a file and remove other contents.
    e.g. if a file contains 10000 lines, i want to keep bottom 100 lines and remove all other 9900 lines to make it small .. without deleting, renaming (moving) files.
    One can do this by tail -100 file1 > file 2 and then mv file2 file1
    But that is not what we want.
    file1 is used by application and is continually updating it and growing.
    Intention is to remove older contents while not disturbing its use by application.


  • Guru


    Use this

  • aa