Cron Job to auto delete folder older than 7 days Linux


I am having issue storing my server backup on a storage VPS. My server is not deleting old backup folders and the storage is getting full and the backup fails in mid way. My runs once every week.

Can anyone help me create a cron job script on that deletes folder older than 7 days and runs one day before backup and delete old folders.

Any help appreciated.


Answers:


For example, the description of crontab for deleting files older than 7 days under the /path/to/backup/ every day at 4:02 AM is as follows.

02 4 * * * find /path/to/backup/* -mtime +7 -exec rm {} \;

Please make sure before executing rm whether targets are intended files. You can check the targets by specifying -ls as the argument of find.

find /path/to/backup/* -mtime +7 -ls

mtime means the last modification timestamp and the results of find may not be the expected file depending on the backup method.