#!/usr/bin/ksh ############################################################################### # Script: hkeeping.sh # Version 0.01 # Shell: !/usr/bin/ksh # Platform: AIX Systems # Description: Delete directories and keeps only the ten newer ones. # Creator: Emerson Posadas # Date: Jul 27th, 2007 ############################################################################### ############################################################################### # Change history: # 07/27/2007.- First script. ############################################################################### #Get the GEO from the command line and convert the string to lowercase GEO=$(print $1 | tr [A-Z] [a-z]) #Number of log files to keep LIMIT=20 #GEO is mandatory to execute if [ "${GEO}" = "" ] then echo "Geo needed to run this script" echo "Correct syntax is hkeeping.sh " exit 1 else if [[ $GEO != @(emea1|emea2|la|na|ap|mmlc) ]] then echo "Please use a valid geo (emea1, emea2, ap, na, la, mmlc)" else #Count the total of geo directories matched count() { TOTAL=$(ls -rt | grep $GEO | wc -l | cut -c 7-8) #TOTAL=$(ls -rt | grep $GEO | wc -l | awk '{print $1}') } cd /foo/bar count #If there is more than $LIMIT directories then delete the older ones if [ $TOTAL -gt $LIMIT ]; then #Number of iterations (directory deletions to do) let ITER=$TOTAL-$LIMIT while [[ $ITER -gt 0 ]] ; do #Select the older element ELEMENT=$(ls -lrt | grep $GEO | awk '{print $9}' | head -1) echo "$ELEMENT deleted" #Delete the older element ls -lrt | grep $GEO | awk '{print $9}' | head -1 | xargs rm -fr let ITER=$ITER-1 done else echo "Cannot delete more log files" exit 1 fi fi fi