Ubuntu Server: How to Empty a Text Log File Tips, Tricks and Tutorials 26 DEC 2013

ubuntu-10-logoWhen it comes to debugging a problem on an Ubuntu Server, often the only way forward is to consult the various log files on the system. Of course, log files tend to be left unattended, meaning they get very large, which in turn means every now and then that you simply need to wipe them so that you can start collecting some fresh debug data.

Naturally you don’t want to tamper with these files too much, but in the event that you need to empty a text file from a terminal window, this is what you do:

cat /dev/null > logfiletoempty.log

In Linux, /dev/null, or the null device, is a special file that discards all data written to it but reports that the write operation succeeded. It provides no data to any process that reads from it, yielding EOF immediately. So looking at the above, we’re reading the empty file and then writing it’s ‘contents’ to the specified file (in this case the log file we want to empty). This action overwrites the existing file with blank content, effectively emptying it.

Nice and simple.

Related Posts:

About Craig Lotter

South African software architect and developer at Touchwork. Husband to a cupcake baker and father to two little girls. I don't have time for myself any more.

  • tehtorq

    You can also: echo “” > logfiletoempty.log