shell script to delete log files

Shell Script to Clear/Delete Log Files

Every server generates log files that keep accumulating over time. If left unchecked, these log files can soon occupy lot of space on your system and affect its performance. So it is advisable to periodically clear/delete log files. Generally, developers and system administrators delete/clear log files once every 30 days, or every 10 days, in case the server receives a lot of traffic. It can be tedious to manually clear/delete log files every time, since it is a routine task. So it is advisable to create a shell script to clear/delete log files to automate this task. In this article, we will learn how to clear/delete log files in Linux.


Shell Script to Clear/Delete Log Files

The find command is one of the most popular tools to find files & folders that are older than ‘x’ number of days. We will look at some commonly used find commands to find old files.


Delete Files older than 1 day

Here is the command to delete files that are older than 1 day.

find /path/to/files* -mtime +1 -exec rm {} \;

Let us look at the above command in detail.

The first argument is the path to files which can be files, directories or even wildcard characters.

For example, if your apache server’s log files are in /var/log/apache, then here is the command to delete log files that are older than 1 day.

find /var/log/apache* -mtime +1 -exec rm {} \;

Next, -mtime is used to specify number of days old your files are. For example, if you specify +1 find command will search for files older than 1 day.

The 3rd argument is -exec that allows you to pass the command to be executed. We use rm command since we want to delete files. The {} \; is used to end the command.

Now we will look at some common use cases for this command.


Delete Files older than 7 days

Here is the command to delete files older than 7 days. Replace /path/to/files with the path to your log files or directory.

find /path/to/files* -mtime +7 -exec rm {} \;


Delete Files older than 30 days or 1 month

Here is the command to delete files older than 30 days. Replace /path/to/files with the path to your log files or directory.

find /path/to/files* -mtime +30 -exec rm {} \;


Delete Files older than 6 months

Here is the command to delete files older than 6 month (182 days). Replace /path/to/files with the path to your log files or directory.

find /path/to/files* -mtime +182 -exec rm {} \;


Delete Files older than 1 year

Here is the command to delete files older than 1 year (365 days). Replace /path/to/files with the path to your log files or directory.

find /path/to/files* -mtime +365 -exec rm {} \;

If you get ‘permission denied’ message while running any of the commands, use sudo keyword before the commands, or switch to a user that has the permission to remove files, and then try the above commands.

Now let us put the above command in a shell script. Create a blank shell script in vi editor.

$ vi /home/ubuntu/delete-log.sh

Add the following lines to it.

#!/bin/sh

log_path="/var/log/apache"

#delete files older than 30 days
find $log_path* -mtime +30 -exec rm {} \;

echo 'log files older than 30 days deleted from $log_path'

Save and close the file. You can customize your shell script by modifying the find command mentioned in it, as per your requirement. We have mentioned several use cases of using find command to search & delete files, above. Similarly, you can also change location of your log files, depending on your server configuration.

Make it executable

$ chmod +x /home/ubuntu/delete-log.sh

Now you can run the shell script with the following command.

$ /home/ubuntu/delete-log.sh

It is better to setup a cron job to run this script daily/weekly. So run the following command to open crontab file.

$ crontab -e

Add the following line it to run the above shell script every day at 10.a.m.

0 10 * * * /home/ubuntu/delete-log.sh >/dev/null 2>&1

Make sure to mention the full path to your shell script in your cronjob command. Save and close the file to create the cronjob.

You can use a crontab generator to create your schedules.

In this article, we have learnt how to create shell script to delete/clear log files.

Also read:

How to Exclude Requests from Apache Log Files
How to Exclude Requests from NGINX Log Files
How to Pass Variables in cURL Command
Shell Script to Restart Service If Not Running
Shell Script to Read Data from File

Leave a Reply

Your email address will not be published. Required fields are marked *