Managing server storage efficiently is crucial for maintaining performance and ensuring that unnecessary files do not take up valuable disk space. One effective way to keep your system clean is by automatically removing old files that are no longer needed. In this guide, we’ll explore how to use a Bash script to delete files matching a specific pattern that are older than 7 days.
Why Automate File Cleanup?
As servers run over time, they accumulate logs, temporary files, and other data that may not be necessary for long-term storage. Regularly cleaning up these files can help:
- Free up disk space
- Improve server performance
- Reduce backup sizes
- Ensure compliance with data retention policies
Rather than manually deleting old files, an automated Bash script can handle the task efficiently.
Writing a Bash Script for File Cleanup
The following script finds and removes files that match a given pattern and are older than 7 days:
|
1 2 3 4 5 6 7 8 9 10 11 12 |
#!/bin/bash # Define the directory and pattern TARGET_DIR="/path/to/folder" PATTERN="*.log" # Modify this pattern as needed # Remove files older than 7 days find "$TARGET_DIR" -type f -name "$PATTERN" -mtime +7 -exec rm -f {} \; # Optional: Print confirmation message echo "Deleted files matching '$PATTERN' older than 7 days in $TARGET_DIR" |
How This Works
find "$TARGET_DIR" -type f→ Searches for files (-type f) in the specified directory.-name "$PATTERN"→ Filters files based on the pattern (e.g.,*.logfor log files).-mtime +7→ Matches files older than 7 days.-exec rm -f {} \;→ Deletes each matching file.- The
echostatement at the end is optional and prints a confirmation message.
Running the Script
- Save the script to a file, e.g.,
cleanup.sh:
12nano cleanup.sh
Paste the script into the file and save it. - Make it executable:
12chmod +x cleanup.sh - Run the script manually:
12./cleanup.sh
Automating Cleanup with Cron
To ensure the cleanup runs automatically, you can schedule it using cron:
- Open the crontab editor:
12crontab -e - Add the following line to run the script every day at 2 AM:
120 2 * * * /path/to/cleanup.sh - Save and exit. The script will now run at the specified time daily.
Additional Customization Options
Delete Files in Multiple Directories
If you want to clean multiple directories, modify the script like this:
|
1 2 3 4 5 6 7 8 9 10 11 |
#!/bin/bash # Define multiple directories DIRECTORIES=("/var/log" "/tmp/cache" "/home/user/backups") PATTERN="*.log" for DIR in "${DIRECTORIES[@]}"; do find "$DIR" -type f -name "$PATTERN" -mtime +7 -exec rm -f {} \; echo "Cleaned $DIR" done |
Log Deleted Files
To keep a record of deleted files, modify the find command:
|
1 2 |
find "$TARGET_DIR" -type f -name "$PATTERN" -mtime +7 -exec rm -f {} \; -exec echo "Deleted: {}" >> /var/log/file_cleanup.log \; |
Conclusion
Automating file cleanup is a simple yet powerful way to maintain a well-optimized server. Using a Bash script along with cron jobs, you can efficiently remove unnecessary files, reclaim disk space, and enhance performance. Customizing the script to fit your specific needs ensures a smooth and automated process without manual intervention.
Need further automation? Consider integrating this with log rotation or cloud storage backups for even more efficiency!
This article was prepared with AI assistance.




