How to empty or clear the contents of an existing UNIX file
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








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.
Jesus E. Aneiros said on October 8, 2008
cp /dev/null blah.txt
It will save you a t and a >.
Jacob said on October 15, 2008
echo -n > file.txt
Quinn McHenry said on October 15, 2008
Nice.
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…
Tech Blog said on April 18, 2009
I use a lot of
echo ” ” > blah.txt
But whatever works will do :)
dinesh said on May 15, 2009
very helpful
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.
Anonymous said on August 29, 2009
Thank you .This thing had worked out well for me
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
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”
Anonymous said on February 17, 2010
Important to note- the ‘> filename’ method will only work on sh, and ksh variants but not csh.
–Dennis
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
DINESH said on May 24, 2010
echo -n > blah.txt
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?
Guest said on August 13, 2010
: > blah.txt
Geo said on November 3, 2010
# touch test
# cat test > blah.txt
this commands will empty the file blah.txt
Vinothkumar Sundaram said on November 9, 2010
Sorry it is working
Duncan Anderson said on December 13, 2010
:> filename
Has always been my choice, especially in shell scripts.
Sandeep D. said on April 18, 2011
Thansk. This is the best way to clear the contents of a file
UNIX PROMPT> :> [file name]
Lakshmanan Samy said on November 12, 2011
Thanks these are all helpful