Ubuntu – Emptying the contents of a text file

command line

I have a text file in my home directory which receives data regularly through a cron job. Because the cron job provides dynamic data, I would also want to set a cron job to empty the file contents (the file must still exist). I don't need help with cron, just the command which can help with emptying the file.

Best Answer

The simplest way:

> filename

This is a less obvious way and probably less readable in a shell script by others, but it's all you need.

Because > is not actually a really command (it is bash builtin) you can't use:

sudo > filename

when you don't have permissions on that file. But you can use:

sudo bash -c "> filename"
Related Question