Raspberry Pi

How to Empty System Log Files on Raspberry Pi

In Linux-based systems such as Raspberry Pi, the system log files are present which contain the process, logins details and other actions. Over time the content inside the log files keeps increasing and it acquires space on the disk. For Raspberry Pi (mini-computer), it is important to keep emptying space on the disk so that the system won’t run out of space.

If you want to free up some space on your Raspberry Pi then it is a good idea to the empty system log files and this article is a guide on how to do that.

How to Display System Log Files in Raspberry Pi

In the Raspberry Pi system, all the log files are present in /var/log directory. So, in order to view the system log files, change the directory to /var/log:

cd /var/log

 
Then by using the ls command, the complete list of log files can be displayed:

ls

 

Empty System Log Files on Raspberry Pi

There are four methods to the empty system log files on Raspberry Pi, which are as follows:

Method 1: Using truncate Command

The first method to empty system log files is by using the “truncate” command. Truncating a file means to empty/delete all its content to make the size of the file 0 kB.

“truncate” command is mostly used to empty system log files. The syntax for the truncate command is mentioned below:

sudo truncate -s 0 <filename>

 
For example; here, I want to remove the content of the auth.log file.


To truncate or empty this file I have run the truncate command as shown below:

sudo truncate -s 0 auth.log

 

After running the above command now when I opened the file, you can see that it’s empty:

Method 2: Using > Command

The Raspberry Pi user can also use “>” command with the system log file name to empty a log file on the system. However, to empty the log file through “>”, the users must switch to root before applying the command.

Here, I want to empty the syslog file which is highlighted in the image below. So firstly, let’s view the /var/log directory in which all our system log files are present:

ls

 

The syslog file will look something like as shown in the image below:


Now let’s switch to root user and delete the syslog file by following the below-written commands:

sudo su root

 
Type:

> syslog

 

To verify, open the syslog file using nano editor it is empty:

Method 3: Using echo command

Third on our list is the echo command, by using the echo command, one can also empty the system log file. Just follow the below-mentioned echo command syntax:

echo > <log-filename>

 
Remember to use this command the user is required to have root privileges or you can simply switch to root user by using sudo su command.

For example:

echo > user.log

 

 

Method 4: Using /dev/null Command

Last method on the list to empty the system log files on Raspberry Pi is by using the /dev/dull command along with the cat command. /dev/null is a special file on Linux based system such as Raspberry Pi, but anything which is deleted using /dev/null can’t be recovered. Just follow the below-mentioned syntax for this method to empty the system log files on Raspberry Pi.

cat /dev/null > <log-file>

 
For example:

cat /dev/null > boot.log

 

 

In this way, you can empty any system log file you want using any method.

Conclusion

There are four ways to empty the system log files: one is by using the “truncate” command to change the size of the file to 0kB, another is by switching to the root user and then emptying the file by using “>” command. Then we have “echo >” command and finally, we have “/dev/null” command.

About the author

Zahra Zamir

An Electronics graduate who loves to learn and share the knowledge, my passion for my field has helped me grasp complex electronics concepts and now I am here to share them with others.