I have a folder /var/backup where a cronjob saves a backup of a database/filesystem. It contains a latest.gz.zip and lots of older dumps which are names timestamp.gz.zip. The folder ist getting bigger and bigger and I would like to create a bash script that does the following:
- Keep latest.gz.zip
- Keep the youngest 10 files
- Delete all other files
Unfortunately, I'm not a good bash scripter so I have no idea where to start. Thanks for your help.
In zsh you can do most of it with expansion flags:
Be careful with this command, you can check what matches were made with:
You should learn to use the
find
command, possibly withxargs
, that is something similar toor if your
find
doesn't have-delete
:Of course you'll need to improve a lot, this is just to give ideas.