Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 280
Sohail Ahmed Avatar asked Jan 01 '17 15:01

Sohail Ahmed


1 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.

like image 139
minamijoyo Avatar answered Oct 02 '22 20:10

minamijoyo