Skip to main content

How to truncate a log file using Ubuntu Linux

Have you ever wanted to truncate a log file that was being written to by a running process without stopping the process to do so?

If so then all you need is the following to clear out the active log file:
cat /dev/null > some_log_file.log
There are some other great tips over on the Bash Cures Cancer blog.

Technorati Tags: , , , , ,


askbill said…
Thanks a bunch. Just what I was looking for.
Anonymous said…
:> filename

(colon greater-than filename) is a much shorter syntax :)
nowinter said…
but, if the file is still being appended by a process, it worth nothing in the sence of file size. the file will be updated to its actual size (with NULs) as soon as the appending process flushes..
Anonymous said…
depending on the log file you might also want to send a HUP to the program that has the file open. Eg. if you truncate /var/log/messages do a 'sudo killall -HUP syslogd'
Nice. Came in handy just now. No need to remember anything anymore in the Google age.
Another useful, er, use for cat /dev/null -- I'm copying files verbosely to my external hard drive. I've noticed a directory that had several versions of the same video: mpg, avi, flv, and dv -- the latter with 9 GB :P I didn't need that one, so _while_it was copying the .dv I went
cat /dev/null > /path/unnecessary-long-file.dv
Thanks for the tip!
and hey presto -- the backup moved on to the next file, and I didn't break the cp command (which would be very hard to resume) (yes, I hear that rsync is a better way of doing this, I'll check it for next time)
Anonymous said…
Brilliant, thanks for this
Anonymous said…
> some_log_file.log
Anonymous said…
There is already a command which is the best way to do this:

truncate -s0 some_log_file.log