Home Linux Commandline Tips How to Empty System Log Files in Linux

How to Empty System Log Files in Linux

Logging is a normal operation that the Linux operating system performs constantly to maintain different types of messages in various log files.

If you’re maintaining a Linux server, it’s most likely that you might have come across an issue of running out of disk space. In such a situation, emptying huge log files mainly resolve the problem.

Using the rm command to directly delete log files is what you should avoid as it can leave you in a messed up situation. In this article, we’ll see various methods to clean up log files in Linux without deleting the actual file entirely.

Create A Sample Log File

Before we jump to the main topic, let’s first create a sample log file on which we’re going to perform operations. The same steps you can follow for your desired log files by using the sudo privileges.

To make a sample log file, you can use the fallocate utility using the below command:

$ fallocate -l 5M app.log

It will give us a file with a 5MB size, which you can verify using the ls command.

$ ls -lh app.log
List Files by Size in Linux
List Files by Size in Linux

Clear Logs in Linux Using Cat Command

Concatenating the popular cat command with the /dev/null device file in Linux, you can easily empty the content of a log file.

In case you don’t know, /dev/null is a special file in Linux that helps in disappearing anything written or streamed to it returning the empty output.

To clear or empty any log file, just issue the following command.

$ cat /dev/null > app.log
$ ls -lh app.log
Clear Log File in Linux
Clear Log File in Linux

As you can see, instead of completely deleting the file, the command only removed the file content making its size zero.

Use Redirection Operator To Clear Log In Linux

Redirection Operator (>) is one of the easiest ways to empty the log files in the Linux operating system. Just using the redirection operator with the log filename on the right side and nothing on the left side redirects Null to the file by making it blank.

$ > app.log
Empty Log File in Linux
Empty Log File in Linux

Empty Log File Using ‘true’ Command In Linux

Attaching the colon (:) symbol to the left of the redirection operator makes another built-in true command that also does the same work as the redirection operator.

You can use it as given below:

$ :> app.log

Likewise, you can add true in place of :> symbol to perform the same task.

$ true app.log

Clear Logs in Linux Using Truncate Command

As the meaning of the name says “removing part of something”, truncate is also yet another Linux utility that helps to free up space by shrinking the size of the file without deleting the file entirely.

You can utilize the truncate Linux command with the -s option that defines the file size to empty a file content. Giving a size of zero (0) is equivalent to making file content NULL or adjusting the file size to 0 bytes.

$ truncate -s 0 app.log
Clear Logs Using Truncate Command
Clear Logs Using Truncate Command

As you can see in the above screenshot, we create a file app.log with a size of 5MB. Then, using a truncate command, we readjusted its size to zero without deleting the file itself.

Clear Log In Linux Using dd Command

I’m sure you must have used the dd (disk/data duplicator) command line utility to create a bootable USB without destroying your disk. The way you copy an image file to a USB boot drive, likewise, you can write blank off to your log file by just changing the input and output file.

$ dd if=/dev/null of=app.log
Clear Logs Using dd Command
Clear Logs Using dd Command

Here, “if” denotes the input file that you want to write to the output file as denoted by “of”.

Truncate Logs In Linux Using Echo Command

The echo command is mainly used to print or send messages in the terminal. The same functionality of the echo command can utilize to send a null output to the log file.

Simply run the below command to redirect the empty to the file:

$ echo "" > app.log
Or 
$ echo > app.log
Truncate Logs In Linux
Truncate Logs In Linux

However, if you see in the above screenshot, the file size is still not zero meaning the file is not completely empty. This is because we redirected an empty string which is not the same as NULL.

So, to send a null output to the file and make file size zero, you also need to use the -n flag with the echo command that restricts any trailing newline or leaving any empty line as happened in the above case.

$ echo -n "" > app.log

Now the file size becomes zero and there is no content in the log file.

Clear Logs In Linux Using Logrotate Tool

Coming to the last and considered one of the best-automated methods, you can also use a logrotate tool that is built specifically to manage logs. It helps in the automatic rotation, compression, and removal of log files.

Check out the separate article on how to rotate logs with Logrotate in Linux for more information.

Finally, we learned to use different command line utilities to clear logs without deleting the files entirely in the Linux operating system. You can explore each command separately to use it along with Cronjob to automate the clean-up of logs at regular intervals of time.

Sarvottam
Sarvottam Kumar is a software engineer by profession with interest and experience in Blockchain, Angular, React and Flutter. He loves to explore the nuts and bolts of Linux and share his experience and insights of Linux and open-source on the web/various prestigious portals.

Each tutorial at UbuntuMint is created by a team of experienced writers so that it meets our high-quality standards.

Was this article helpful? Please add a comment to show your appreciation and support.

Got something to say? Join the discussion.

Thanks for choosing to leave a comment. Please keep in mind that all comments are moderated according to our comment policy, and your email address will NOT be published or shared. Please Do NOT use keywords in the name field. Let's have a personal and meaningful conversation.