I am using the following command to delete old backup files:
find backups/ -name "bak_*.tbz" -mmin +4800 -exec rm -vf {} \;
The problem: When there are no new files, than all files are deleted. I would likke to keep the newest one in such case.
What is an elegant way to delete old files except the newest one?
This assumes GNU
find
and will print the modification time and file name, sort this by the mod time, delete the first line (newest file), remove the mod time column and hand this over toxargs
for deletion (as stated, it will only echo the delete command, not actually do it).