I have a backup script that runs once a day. It saves the backups in below the backup folder in a subfolder named by date ($(date +"%F")
).
Another script for cleanup is meant to run once a week. I want this script to keep the newest 10 backups and delete all others.
Currently I simply delete everything older than 10 days by calling find $PWD -type d -ctime +10 -exec rm -rf {} +
. But this could destroy all backups if there weren't any backups in the last 10 days because of errors that haven't been realized (which should not but may happen).
If the script would keep the number of available backups at 10 or more, nothing (that) dangerous would happen.
So how to keep only the newest x subfolders and delete all others