Note: This article was originally published in 2015. Some steps, commands, or software versions may have changed. Check the current Ubuntu documentation for the latest information.

In this step-by-step guide, you’ll learn erase a log file in ubuntu. Ubuntu is a popular Linux distribution based on Debian, widely used for servers and development environments.

Prerequisites

Before you begin, make sure you have:

  • A system running Ubuntu (desktop or server edition)
  • Terminal access with sudo privileges
  • Basic familiarity with Linux command line

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:

**>error.log**

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**

Summary

You’ve successfully learned erase a log file in ubuntu. If you run into any issues, double-check the prerequisites and ensure your Ubuntu environment is properly configured.