Table of Contents
In this article I will show example of cronjob for Linux to delete files older than 10 days.
Edit the crontab file
To edit or create new crontab file, type the following command:
crontab -e
Cronjob to delete files older than X days
Note
You should be extremely careful and double check the files you're about to delete.
In crontab editor, add the following line:
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.
An example, we’ll delete all nginx log files older than 10 days.
0 0 * * * /usr/bin/find /var/log/nginx -name "*.log*" -type f -mtime +10 -exec rm -f {} \;
Each line of a crontab file represents a job, and looks like this:
# ┌───────────── minute (0 - 59)
# │ ┌───────────── hour (0 - 23)
# │ │ ┌───────────── day of the month (1 - 31)
# │ │ │ ┌───────────── month (1 - 12)
# │ │ │ │ ┌───────────── day of the week (0 - 6) (Sunday to Saturday;
# │ │ │ │ │ 7 is also Sunday on some systems)
# │ │ │ │ │
# │ │ │ │ │
# * * * * * <command to execute>
You can read more about cronjob
ADVERTISEMENT
5/5 - (1 vote)