Bash Script to Delete Files/Folders Older Than “X” Days in Linux

Disk Usage Monitoring tools are capable of alerting us when a given threshold is reached.

But they don’t have the ingenuity to fix the disk usage problem on their own.

Manual intervention is needed to solve the problem.

But if you want to fully automate this kind of activity, what you will do.

Yes, it can be done using the bash script.

This script prevents alerts from monitoring tool because we delete old log files before filling the disk space.

We have added many useful shell scripts in the past. If you want to check them out, go to the link below.

I’ve added two bash scripts to this article, which helps clear up old logs.

1) Bash Script to Delete a Folders Older Than “X” Days in Linux

We have a folder named “/var/log/app/” that contains 15 days of logs and we are going to delete 10 days old folders.

$ ls -lh /var/log/app/

drwxrw-rw- 3 root root  24K Oct  1 23:52 app_log.01
drwxrw-rw- 3 root root  24K Oct  2 23:52 app_log.02
drwxrw-rw- 3 root root  24K Oct  3 23:52 app_log.03
drwxrw-rw- 3 root root  24K Oct  4 23:52 app_log.04
drwxrw-rw- 3 root root  24K Oct  5 23:52 app_log.05
drwxrw-rw- 3 root root  24K Oct  6 23:54 app_log.06
drwxrw-rw- 3 root root  24K Oct  7 23:53 app_log.07
drwxrw-rw- 3 root root  24K Oct  8 23:51 app_log.08
drwxrw-rw- 3 root root  24K Oct  9 23:52 app_log.09
drwxrw-rw- 3 root root  24K Oct 10 23:52 app_log.10
drwxrw-rw- 3 root root  24K Oct 11 23:52 app_log.11
drwxrw-rw- 3 root root  24K Oct 12 23:52 app_log.12
drwxrw-rw- 3 root root  24K Oct 13 23:52 app_log.13
drwxrw-rw- 3 root root  24K Oct 14 23:52 app_log.14
drwxrw-rw- 3 root root  24K Oct 15 23:52 app_log.15

This script will delete 10 days old folders and send folder list via mail.

You can change the value “-mtime X” depending on your requirement. Also, replace your email id instead of us.

# /opt/script/delete-old-folders.sh

#!/bin/bash
prev_count=0
fpath=/var/log/app/app_log.*
find $fpath -type d -mtime +10  -exec ls -ltrh {} \; > /tmp/folder.out
find $fpath -type d -mtime +10  -exec rm -rf {} \;
count=$(cat /tmp/folder.out | wc -l)
if [ "$prev_count" -lt "$count" ] ; then
MESSAGE="/tmp/file1.out"
TO="[email protected]"
echo "Application log folders are deleted older than 15 days" >> $MESSAGE
echo "+----------------------------------------------------+" >> $MESSAGE
echo "" >> $MESSAGE
cat /tmp/folder.out | awk '{print $6,$7,$9}' >> $MESSAGE
echo "" >> $MESSAGE
SUBJECT="WARNING: Apache log files are deleted older than 15 days $(date)"
mail -s "$SUBJECT" "$TO" < $MESSAGE
rm $MESSAGE /tmp/folder.out
fi

Set an executable permission to “delete-old-folders.sh” file.

# chmod +x /opt/script/delete-old-folders.sh

Finally add a cronjob to automate this. It runs daily at 7AM.

# crontab -e

0 7 * * * /bin/bash /opt/script/delete-old-folders.sh

You will get an output like the one below.

Application log folders are deleted  older than 20 days
+--------------------------------------------------------+
Oct 11 /var/log/app/app_log.11
Oct 12 /var/log/app/app_log.12
Oct 13 /var/log/app/app_log.13
Oct 14 /var/log/app/app_log.14
Oct 15 /var/log/app/app_log.15

2) Bash Script to Delete a Files Older Than “X” Days in Linux

We have a folder named “/var/log/apache/” that contains 15 days of logs and we are going to delete 10 days old files.

The articles below are related to this topic, so you may be interested to read.

# ls -lh /var/log/apache/

-rw-rw-rw- 3 root root  24K Oct  1 23:52 2daygeek_access.01
-rw-rw-rw- 3 root root  24K Oct  2 23:52 2daygeek_access.02
-rw-rw-rw- 3 root root  24K Oct  3 23:52 2daygeek_access.03
-rw-rw-rw- 3 root root  24K Oct  4 23:52 2daygeek_access.04
-rw-rw-rw- 3 root root  24K Oct  5 23:52 2daygeek_access.05
-rw-rw-rw- 3 root root  24K Oct  6 23:54 2daygeek_access.06
-rw-rw-rw- 3 root root  24K Oct  7 23:53 2daygeek_access.07
-rw-rw-rw- 3 root root  24K Oct  8 23:51 2daygeek_access.08
-rw-rw-rw- 3 root root  24K Oct  9 23:52 2daygeek_access.09
-rw-rw-rw- 3 root root  24K Oct 10 23:52 2daygeek_access.10
-rw-rw-rw- 3 root root  24K Oct 11 23:52 2daygeek_access.11
-rw-rw-rw- 3 root root  24K Oct 12 23:52 2daygeek_access.12
-rw-rw-rw- 3 root root  24K Oct 13 23:52 2daygeek_access.13
-rw-rw-rw- 3 root root  24K Oct 14 23:52 2daygeek_access.14
-rw-rw-rw- 3 root root  24K Oct 15 23:52 2daygeek_access.15

This script will delete 10 days old files and send folder list via mail.

You can change the value “-mtime X” depending on your requirement. Also, replace your email id instead of us.

# /opt/script/delete-old-files.sh

#!/bin/bash
prev_count=0
fpath=/var/log/apache/2daygeek_access.*
find $fpath -type f -mtime +15  -exec ls -ltrd {} \; > /tmp/file.out
find $fpath -type f -mtime +15  -exec rm -rf {} \;
count=$(cat /tmp/file.out | wc -l)
if [ "$prev_count" -lt "$count" ] ; then
MESSAGE="/tmp/file1.out"
TO="[email protected]"
echo "Apache Access log files are deleted older than 20 days"  >> $MESSAGE
echo "+--------------------------------------------- +" >> $MESSAGE
echo "" >> $MESSAGE
cat /tmp/file.out | awk '{print $6,$7,$9}' >> $MESSAGE
echo "" >> $MESSAGE
SUBJECT="WARNING: Apache log folders are deleted older than 15 days $(date)"
mail -s "$SUBJECT" "$TO" < $MESSAGE
rm $MESSAGE /tmp/file.out
fi

Set an executable permission to “delete-old-files.sh” file.

# chmod +x /opt/script/delete-old-files.sh

Finally add a cronjob to automate this. It runs daily at 7AM.

# crontab -e

0 7 * * * /bin/bash /opt/script/delete-old-folders.sh

You will get an output like the one below.

Apache Access log files are deleted older than 20 days
+--------------------------------------------------------+
Oct 11 /var/log/apache/2daygeek_access.11
Oct 12 /var/log/apache/2daygeek_access.12
Oct 13 /var/log/apache/2daygeek_access.13
Oct 14 /var/log/apache/2daygeek_access.14
Oct 15 /var/log/apache/2daygeek_access.15

Magesh Maruthamuthu

Love to play with all Linux distribution

You may also like...