Master Linux File Cleanup: Automate Deletion with find and cron
This guide explains how to use the Linux find command to locate old files, create a shell script for batch deletion, and schedule the script with crontab, including detailed syntax, examples, and common cron expression patterns for reliable automated cleanup.
Using find for Automated File Deletion
The find command can search directories based on modification time, name patterns, and execute actions on matched files. A typical one‑liner to delete files older than a given number of days is:
find /usr/local/backups -mtime +10 -name "*.*" -exec rm -rf {} \;Explanation of the options:
/usr/local/backups : target directory.
-mtime +10 : select files modified more than 10 days ago.
-name "*.*" : match any file containing a dot; replace with *.jpg or * for other patterns.
-exec rm -rf {} \; : run rm -rf on each matched file (the braces represent the file name).
Creating a Shell Script and Scheduling It with cron
To run the cleanup regularly, create an executable script, place it in a known location, and add a cron job.
# Create the script file
touch /usr/local/bin/clear
chmod 777 /usr/local/bin/clear
# Edit the script
vi /usr/local/bin/clear
#!/bin/sh
find /usr/local/backups -mtime +10 -name "*.*" -exec rm -rf {} \;After saving, schedule it with crontab -e: * 2 * * * /usr/local/bin/clear This entry runs the script every day at 02:00 AM.
Understanding the Crontab File Format
A crontab line consists of six fields: five time specifications followed by the command to execute.
Minute (0‑59)
Hour (0‑23)
Day of month (1‑31)
Month (1‑12)
Day of week (0‑6, where 0 = Sunday)
Command
Common patterns: * * * * * – every minute. a‑b – a range (e.g., 0‑5 minutes). */n – every n units (e.g., */2 every two hours). a,b,c – specific values (e.g., 0,15,30,45 minutes).
Practical Cron Examples
0 */2 * * * /sbin/service httpd restart– restart Apache every two hours. 50 7 * * * /sbin/service sshd start – start SSH at 07:50 daily. 0 0 1,15 * * fsck /home – run filesystem check on the 1st and 15th of each month. 1 * * * * /home/bruce/backup – execute backup script at the first minute of every hour. 00 03 * * 1-5 find /home "*.xxx" -mtime +4 -exec rm {} \; – delete *.xxx files older than four days, Monday‑Friday at 03:00. 30 6 */10 * * ls – run ls on the 1st, 11th, 21st, and 31st of each month at 06:30.
To view the current crontab, use crontab -l. To edit, use crontab -e. After adding entries, ensure the cron daemon is running (e.g., service crond restart on CentOS).
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
ITPUB
Official ITPUB account sharing technical insights, community news, and exciting events.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
