How to: Erase a log file in Ubuntu

How to: Erase a log file in Ubuntu

Ever had this huge error log, full of nightmares and bad memories? Well, once you’re done fixing the problems then you’re stuck with megs if not gigs worth of bad memories that perhaps you wish to get rid off. Well, in my case one of my errors sent so much information to the log file it was on the gigs arena. This obviously becomes an issue as much of that information is not important to me any more and it can have a performance impact on the system.

Usually I would move the original file to a backup location and create a new one. The issue with this approach is that the permissions or attributes of the original file need to be copied over to the new one… more work basically. So I did some research and it is possible to simply truncate your log file so that all you’re doing is getting rid of the data within and keeping all the attributes and the like untouched.

There are a couple of ways to do this: You can do some fancy output redirection or you could use the truncate command. Either way the result is the same and all it takes is one command line. Here it goes:

Option 1: Redirect output to the file to clear it:

If you simply redirect null output to the file, basically you’re wiping it clean. Simply do the following:


Option 2: Use the truncate command:

For those who feel more at home with a traditional command instead of ninja magic:

truncate -s0 error.log

You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.