opened image

Cleaning up old logs and temporary files on Linux servers

Cleaning old logs.

 

To maintain the cleanliness and optimal operation of the system, it is necessary to periodically clean old logs. Below are the actions that can be taken for this purpose.

 

1. Cleaning system logs:


It is necessary to delete only old log archives, while the logs themselves should not be deleted to avoid errors with the operation of some services. It is advisable to compress or clear logs using the following commands:

find /var/log \( -name "*.[0-9]" -o -name "*.*.[0-9]" -o -name "*.gz" \) -exec rm {} \;
find /var/log/ -type f -name "*log" -exec cp /dev/null {} \;

 

If your server generates a lot of logs daily, and there is not enough free space on the disk, you can perform daily cleaning of outdated log archives. For this, you can add the following command to the crontab:

0 0 * * * find /var/log \( -name "*.[0-9]" -o -name "*.*.[0-9]" -o -name "*.gz" \) -exec rm {} \; && find /var/log/ -type f -name "*log" -exec cp /dev/null {} \;

 

2. Cleaning the repository cache:


It is recommended to use special commands to clean the local repository cache of downloaded package files, instead of manually deleting the cache in the /var/cache/ directory. Here are some of them:

For CentOS/RedHat systems based on yum:

yum clean all

For Ubuntu/Debian systems based on apt-get:
 

apt-get clean

 

3. Deleting temporary files:

​​​​​​​
When deleting temporary files using the following commands, care should be taken not to delete important data, such as backups of configs, websites, or databases that may be stored in temporary folders. First, check the contents of these folders using the command `ls -la`:

ls -la /tmp/
ls -la /var/tmp/

If you have checked and there is nothing important there, you can use the following commands to delete temporary files:

rm -rf /tmp/*
rm -rf /var/tmp/*

 

4. Additional actions for servers with different control panels:

ISPmanager:

 

Clear (reset) all ISP log files:

find /usr/local/mgr5/var/logs -type f -exec sed -i 'd' "{}" \;

 

Clear log archives in the /var/www/httpd-logs directory:

find /var/www/httpd-logs \( -name "*.[0-9]" -o -name "*.*.[0-9]" -o -name "*.gz" \) -exec rm {} \; && find /var/www/httpd-logs/ -type f -name "*log" -exec cp /dev/null {} \;

Folders with temporary files in ISPmanager:

rm -rf /var/www/www-root/data/tmp/*

Where www-root is the default user, or another user used on the server.

 

VestaCP:

 

Delete temporary files and incomplete archives in the VestaCP directory:

rm -rf /home/admin/tmp*
rm -rf /backup/tmp.*

This deletes incomplete or unsuccessfully executed archives of the admin user. For another user, specify them in the command.

In VestaCP, you can also use the following command to delete outdated log archives:

rm -rf /var/log/*-2023*.gz

Where you specify the current year instead of 2023.

Conclusions.

These actions will help clean the system of outdated and unnecessary logs and temporary files, maintaining its functionality and optimization.


We also suggest considering other useful articles: