How to clear the contents of a file from the command line?

In bash, just

> filename

will do. This will leave you with an empty file filename.

PS: If you need sudo call, please consider to use truncate as answered here.


You can use the user command : truncate

truncate -s 0 test.txt

("-s 0" to specify the size)

http://www.commandlinefu.com/commands/view/12/empty-a-file


You could do this:

echo -n "" > file.log

Using > to write the (null) input from echo -n to the file.

Using >> would append the null input to the file (effectively doing nothing but touching it).