Disk Space Cleanup Shell Script

This is clean up script which you can use as a standard. Test it from your end before running it in production servers:

dir_cleanup.sh

start_dte=`date ‘+%X %x’`
this_server=$(uname -a |cut -d” ” -f2)

# command line

if [ $# -ne 1 ]
then
echo “Usage: $0
exit 1
fi

env_file=$1

# Source in the Env file
. ${env_file}

#########################################################################
# function to cleanup the directory
#########################################################################

function do_cleanup
{
#
# Remove older than $bkp_d days
#

for dir in $bkp_dir1;
do
echo “Clean $dir”
find $dir -mtime +$bkp_d1 -type f -name ‘*’ -print -exec rm -f {} \;
done

for dir in $bkp_dir2;
do
echo “Clean $dir”
find $dir -mtime +$bkp_d2 -type f -name ‘*’ -print -exec rm -f {} \;
done

for dir in $bkp_arch;
do
echo “Clean $dir”
find $dir -mtime +$bkp_d4 -type f -name ‘*’ -print -exec rm -f {} \;
done

for dir in $bkp_log_dir;
do
echo “Clean $dir”
find $dir -mtime +$bkp_d3 -type f -name ‘*’ -print -exec rm -f {} \;
done

}

#########################################################################
# Call function to cleanup the directory
#########################################################################

do_cleanup

echo Stop $0 `date ‘+%X %x’`

The env file for the path which need to be cleaned:

dir_cleanup.env

bkp_d1=35
bkp_d2=45
bkp_d3=10
bkp_d4=50

bkp_dir1=”
/path/

#bkp_arch=””

Execute the above in cron as given below:

/home/dir_cleanup.sh /home/cdv/env/dir_cleanup.env > /var/logs/dir_cleanup_log.$(date +’%Y%m%d_%H%M%S’)

Leave a Reply

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.