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.logThere are some other great tips over on the Bash Cures Cancer blog.
Technorati Tags: Truncate, Log, File, Ubuntu, Linux, Andrew Beacock


Buy Stuff From Amazon 
9 Comments:
Thanks a bunch. Just what I was looking for.
:> filename
(colon greater-than filename) is a much shorter syntax :)
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..
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)
Brilliant, thanks for this
> some_log_file.log
There is already a command which is the best way to do this:
truncate -s0 some_log_file.log
Post a Comment