How to clean log files in Linux

 

Before you begin, ensure that you are logged in to the terminal as the root user.

1.Check the disk space from the command line. Use the du command to see which files and directories consume the most space inside of the /var/log directory.

#du -h /var/log/

The du command prints the estimated disk space usage of each file and directory for the path that you specified.

The -h argument causes the command to print the information in a human-readable format.

The output of the du command  :

[email protected] [~]# du -h /var/log/

24K    /var/log/cups

16K    /var/log/mail

36K    /var/log/prelink

19M    /var/log/audit

84K    /var/log/bandwidth/2011/Jun

128K   /var/log/bandwidth/2011/Jan

116K   /var/log/bandwidth/2011/Feb

712K   /var/log/bandwidth/2011

4.5M   /var/log/bandwidth

2.6G   /var/log/munin

8.0K   /var/log/conman.old

8.0K   /var/log/pm

8.0K   /var/log/conman

12K    /var/log/dcpumon/boot.1308161402

5.3M   /var/log/dcpumon

8.0K   /var/log/vbox

3.7G   /var/log/

 

2.Select the files or directories that you want to clear:

The /var/log/munin directory uses 2.6 G of space, and is the second largest log on the list.

Use the cd command to move the prompt to the /var/log/munin/ directory. Then, use the du -h * command to see the file sizes.

The du -h command, without the asterisk, shows the directory's size:

 

[email protected] [~]# cd /var/log/munin/

[email protected] [/var/log/munin]# du -h *

603M   munin-graph.log

385M   munin-html.log

67M    munin-limits.log

99M    munin-node.log

1.5G   munin-update.log

 

The du -h command, without the asterisk, shows only directory's size

[email protected] [/var/log/munin]# du -h

2.7 G.

 

3.Empty the files

Use the cat command (concatenate) to empty the log files or directories.

 

#cat /dev/null > munin-update.log  

 

- /dev/null is a non-existent file with no information.

- When you concatenate /dev/null to a log file, you empty the file data, but do not delete the file name.

The output from the previous example shows that the munin-update.log file occupies 1.5G of space in the drive.

To empty this file, use the cat command.

The output should resemble the following example:

[email protected] [/var/log/munin]# cat /dev/null > munin-update.log

[email protected] [/var/log/munin]#

 

To confirm that you successfully emptied the file, use the du -h * command.

[email protected] [/var/log/munin]# cat /dev/null > munin-update.log

[email protected] [/var/log/munin]# du -h *

603M   munin-graph.log

385M   munin-html.log

67M    munin-limits.log

99M    munin-node.log

0M     munin-update.log