The disk usage monitoring tool can alert us when a given threshold has been reached. But they cannot solve the disk usage problem by themselves. Manual intervention is required to resolve the problem.

What would you do if you wanted to fully automate such operations? Yes, you can do this using bash scripts.

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

We used to do a lot of shell scripting. To check it out, go to the link below.

  • How do I use shell scripts to automate everyday activities?

I’ve added two bash scripts to this article that help clean up old logs.

1) Delete the Bash script for folders older than “X” days in Linux

We have a folder called /var/log/app/ that contains 15 days’ worth of logs and we will delete folders that are older than 10 days.

$ 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
Copy the code

The script deletes folders that are 10 days old and sends a list of folders by mail.

You can change the value of -mtime X as needed. Also, please replace your email address with ours.

# /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
Copy the code

Delete -old-folders. Sh set executable permission.

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

Finally, add a cronJob to automate this task. It runs every morning at 7 a.m.

# crontab -e

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

You should see output similar to the following.

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
Copy the code

2) The Bash script that deletes files older than “X” days in Linux

We have a folder called /var/log/apache/that contains 15 days’ worth of logs and we will delete files that are 10 days old.

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

  • How do I find and delete files in Linux that are older than “X” and “X” hours?
  • How do I find recently modified files/folders in Linux
  • How to automatically delete or clean the contents of/TMP folder in Linux?
# 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
Copy the code

The script will delete files that are 10 days old and mail a list of folders.

You can change the value of -mtime X as needed. Also, please replace your email address with ours.

# /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
Copy the code

Set the executable permission to delete-old-files.sh.

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

Finally, add a cronJob to automate this task. It runs every morning at 7 a.m.

# crontab -e

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

You should see output similar to the following.

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
Copy the code

Via: www.2daygeek.com/bash-script…

Magesh Maruthamuthu (lujun9972

This article is originally compiled by LCTT and released in Linux China