How to Recursively Delete Archives on Linux
- 19 January 2016
- Volodymyr Hodiak
- Administration
- 5563
We've got a problem – the storage on one of the servers was full and this caused the overflow on another server. Since at first, a local archive is created and then it is moved to one of the backup servers.
But we, administrators, are lazy people and are not going to browse directories manually and delete archives created several moths ago. Especially, if there are over 200 projects. The simple script will come to help, you will need to use the crontab command.
Script:
find /backup/ -type f -mtime +30 -name "*.tar.gz" -exec rm-f {} \;
where
/backup/ - directory with backups;
-type f — f stands for file;
-mtime +30 — oldr than 30 days;
-name "*.tar.gz" - extension .tar.gz (so not to delete something important!!!);
exec rm -f {} \; - command to remove.
In other words, the command can be interpreted as: find all .tar.gz files older than 30 days and delete them.