I'm trying to get crontab to automatically remove backup folders in a directory apart from the most recent 6 folders as a backup is created in the directory every day.
All my backup folders are just datestamps e.g: 2017_12_29.
When I run
rm -rf $(ls -1 /BACKUPDIR/ | head -n -6) from a terminal it deletes all folders except the most recent 6 which is what I expect it to do, but it doesn't seem to do anything when crontab runs it and nothing is deleted from the directory.
I'm not sure where I'm going wrong here.
EDIT: Currently I'm just using rsync to clone the / directory excluding a few directories.
This is what it looks like in root's crontab:
00 22 * * * /usr/bin/rsync -aqvHAXS --exclude /dev/ --exclude /proc/ --exclude /sys/ --exclude /tmp/ --exclude /run/ --exclude /mnt/ --exclude /media/ --exclude /lost+found/ --delete / /BACKUPDIR/$(date +\%Y_\%m_\%d)