Sometimes disks fill up with logs for example, and we the administrators have to
login sometimes even wake up just to delete log files! Well this script solves the problem.
In this case I used to cleanup Glassfish log files. Just setup it on cron with X amount
of days to keep files. Carefull because its recursive.

The code:

#!/bin/bash
# Script to Deleting old log files
# By Felipe Ferreira
EXPECTED_ARGS=1
if [ $# -ne $EXPECTED_ARGS ]
then
  echo "Script to delete old log files, specify how many olds to keep"
  echo "Usage: `basename $0` "
  exit 2
fi
day=`date`
olderthen=$1
#EDIT HERE
space_before=`df -h |grep "/usr/local"|awk {'print $4'}`
logdir="/usr/local/GlassFishESBv22/glassfish/domains/domain1/logs"
#FINISH EDIT
echo  ". Deleting files older then $olderthen days " >> $logdir/logcleanup.txt
echo  ". On $day" >> $logdir/logcleanup.txt
echo  ". Deleting old files in $logdir ">> $logdir/logcleanup.txt
find $logdir  -name '*.log*' -type f -mtime +$olderthen -exec rm -rfv {} ; >> $logdir/logcleanup.txt
#EDIT HERE
space_after=`df -h |grep "/usr/local"|awk {'print $4'}`
echo  ". Before had $space_before used, now $space_after used. " >> $logdir/logcleanup.txt
Tags: , , , , , ,

1 thought on “File cleanup script for Linux

Leave a Reply

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