Guide on how to delete files and folders using cron within a certain time duration on Linux

Deleting files or folders in Linux ubuntu / centos OS can be done utilizing cron facilities and bash scripts.

input the command to open the cron page:

crontab -e


file contents as follows:

0 0 * * * find /destination-folder-to/ -type d -name '*' -mtime +5 -exec rm -rv {} +

 


0 0 * * * :
This means that this command will be run every day at 00:00 in the morning. More details can learn cron on the website https://crontab.guru/

/destination-folder-to/ :
Specify which folders you want to automatically delete for a certain duration of time.

+5 :
This means that files and folders that are 5 days old will be deleted.

 

Did you find it helpful? Yes No

Send feedback
Sorry we couldn't be helpful. Help us improve this article with your feedback.