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.

Delete Files Older Than 7 Days

   

If you like what you are reading, please:

Buy me a coffeeBuy me a coffee
Summary
Cron Job for Linux To Delete Files Older Than X Days
Article Name
Cron Job for Linux To Delete Files Older Than X Days
Description
How to delete old files in Linux using cron job. Example of cron job for Linux to delete files older than 10 days. Cron job will run every day
Author
Publisher Name
ARSTECH
Publisher Logo

arstech

Leave a Reply

Your email address will not be published. Required fields are marked *