Tentou escrever um script Bash. Por favor, verifique se isso pode ajudá-lo.
#!/bin/bash
NOW='date +%s'
last_day()
{
case $MM in
01|03|05|07|08|10|12 )
echo "31" ;;
04|06|09|11 )
echo "30" ;;
02 )
echo "28" ;;
esac
}
for file in 'ls -1 bkp*'
do
# echo $file
DATE='echo $file | tr -cd [0-9]' # Extract Day of the Month from file name
YY='echo $DATE | cut -c5-8' # Extract Year from file name
MM='echo $DATE | cut -c3-4' # Extract Month of Year fro file name
DD='echo $DATE | cut -c1-2'
FDATE='echo ${YY}${MM}${DD}' # Re-arrange the date
FDATE='date +%s -d $FDATE' # Date in terms of seconds
# echo "FILEDATE= $FDATE"
DIFF='echo "$NOW - $FDATE" | bc' # Difference between NOW and FILEDATE in Seconds
# echo "DIFF FROM NOW= $DIFF"
DIFFDAYS='echo "$DIFF / 86400" | bc' # Difference in terms of DAYS
DIFFMONTHS='echo "$DIFFDAYS / 30" | bc' # Difference in terms of MONTHS
# echo "DIFF IN DAYS= $DIFFDAYS"
# echo "DIFF IN MONTHS = $DIFFMONTHS"
LASTDAY=$(last_day $MM) # To determine whether it is a last day of month
# echo $LASTDAY
if [ $DIFFMONTHS -ge 1 ]
then
if [ $DD -eq $LASTDAY -a $DIFFMONTHS -ge 4 ]
then
rm -rf $file
else
rm -rf $file
fi
fi
done
Saída de teste:
Antes de executar o script:
$ ls
bkp_20082013 bkp_20082014 bkp_20112014
bkp_20071989 bkp_20092014 bkp_30112014
bkp_20072014 bkp_20102014 bkp_31102014
Depois de executar o script:
bkp_20112014 bkp_30112014