Cron Job for Linux To Delete Files Older Than X Days

In this article I will show example of cron job for Linux to delete files older than 10 days.

How to Edit Crontab file

To edit or create new crontab file, type the following command:

$ crontab -e

Cron Job To Delete Files Older Than X Days

In crontab editor type:

0 0 * * * /usr/bin/find /target_directory -name "*.txt" -type f -mtime +10 -exec rm -f {} \;

Change /target_directory and “*.txt” as you need.

Cron expression 0 0 * * * means cron job will run every day at 12:00 AM.

cron job schedule format
cron job schedule format

You can read more about cronjob.

Discuss article in ArsTech Forum

Delete Files Older Than 7 Days

   

If you like what you are reading, please:

Buy me a coffeeBuy me a coffee

arstech

Start the discussion at forum.arstech.net