Delete files older than 3 months.(read date from the name of the file)

Guys,
My log files stored in the date format format below(log_20080714072942):

TIMESTAMP=`date +%Y%m%d%H%M%S`
LOG=/log/log_${TIMESTAMP}.log

I'm looking for a shell script which deletes all files which is older than 3 months from today.

Regards,
Bhagat

IM not gonna write the whole script for you but this should get you started:

#Current Day of Month
d=`date +%d`

#current Month
m=`date +%m`

#current Year on 4 digit format
y=`date +%Y`

TARGET=$(date -u -d '2008-07-13 00:00:00' '+%F %T.%N %Z')
CURRENT=$(date -u -d'$Y-$m-$d 00:00:00' '+%F %T.%N %Z')

#function to get the diff of 2 dates
diff () {
printf '%s' $(( $(date -u -d"$TARGET" +%s) -
$(date -u -d"$CURRENT" +%s)))
# %d = day of month.
}

# $DAYS will equal the diff of $CURRENT and $TARGET, Below numbers are 60 = Minutes per hour, 24 = Hours perday
DAYS=$(( $(diff) / 60 / 60 / 24 ))

Also just an example, with mysql :slight_smile:

mirus scripts # logdate=`echo log_20080214072942 | egrep  -o 'log_[0-9]{6}' | egrep -o '[0-9]{6}'` 
mirus scripts # echo $logdate
200802
mirus scripts # month_diff=`mysql -u root -sABNe "SELECT PERIOD_DIFF(DATE_FORMAT(CURDATE(), '%Y%m'), '$logdate')"`
mirus scripts # echo $month_diff
5



If the date on the file name is the same as the last modified time you may use.

find . -name '*.log' -mtime +90 -exec rm {} \;

-rw-r--r-- 1 owner group 10322 Jul 14 23:47 log.071408