I Need to delete directories in a bash script that are older than 1 year based off of the structure of the folders. The folder structure is year/month/day for example backups/2016/nov/03 or backups/2017/jan/02. How can I loop through the folders and delete the ones that are older than a year?
This is as far as i've gotten
TIMESTAMP=`date +%F_%H%M%S`
BACKUPBASEDIR=/data/pgsql/data/backup
HOSTNAME=`hostname`
YEAR=`date +%Y`
MONTH=`date +%b`
DAY=`date +%d`
LASTYEAR=$(( $YEAR - 1 ))
YEARINT=`printf '%d\n' "$YEAR"`
BACKUPDIR=$BACKUPBASEDIR/$LASTYEAR/$MONTH/$DAY
for f in $BACKUPBASEDIR/*
do
[ $(printf '%d\n' "$f") - $YEARINT -gt 1]; then
echo "i'm not sure what to do at this point"
fi
done
exit
Edit:
$ ls -al
total 12
drwx------. 3 postgres postgres 4096 Oct 25 2016 .
drwx------. 5 postgres postgres 4096 Dec 1 2016 ..
drwx------. 2 postgres postgres 4096 Oct 25 2016 25
The directory 25 has a modified date of Oct 25 2016, but when I run
find /data/pgsql/data/backup/ -ctime +365 -type d
Nothing is found, even through the directory named 25 clearly hasn't been modified for more than a year.