Deleting Files Older Than 10 Days Using Shell Script in Unix Systems

Nov 21, 2025 · Programming · 10 views · 7.8

Keywords: Shell Script | File Deletion | find Command | Unix Systems | crontab

Abstract: This article provides a comprehensive guide on using the find command to delete files older than 10 days in Unix/Linux systems. Starting from the problem context, it thoroughly explains key technical aspects including the -mtime parameter, file type filtering, and safe deletion mechanisms. Through practical examples, it demonstrates how to avoid common pitfalls and offers multiple implementation approaches with best practice recommendations for efficient and secure file cleanup operations.

Problem Context and Requirements Analysis

In Unix/Linux system administration,定期清理过期文件是常见的运维需求。Regular cleanup of outdated files is a common operational requirement. Users need to delete script files in a specified directory that were created more than 10 days ago. These files follow the naming format YYYY.MM.DD.HH_MM_SS.script, such as 2012.11.21.09_33_52.script. The cleanup task needs to be automatically executed every 10 days via crontab, requiring dynamic calculation based on the current date.

Core Solution: Detailed Explanation of find Command

The find command is a powerful tool in Unix/Linux systems for file search and operations, particularly suitable for time-based file cleanup tasks. Here is the complete solution:

find ./my_dir -mtime +10 -type f -delete

Parameter Analysis

Safe Operation Practices

In production environments, directly using the -delete parameter can be risky. A step-by-step verification approach is recommended:

# Step 1: Preview files that will be deleted
find ./my_dir -mtime +10 -type f -print

# Step 2: Execute deletion after confirmation
find ./my_dir -mtime +10 -type f -delete

This method helps avoid accidental deletion of important files, which is particularly crucial during initial script deployment.

Alternative Approaches Comparison

Besides using the -delete parameter, the same functionality can be achieved through the -exec parameter combined with the rm command:

find ./my_dir -mtime +10 -type f -exec rm -f {} \;

This approach offers better compatibility, especially in older Unix system versions. However, correct syntax for the -exec parameter must be ensured to avoid "missing argument to `-exec'" errors.

Advanced Application Scenarios

Filename Pattern Matching

If only files with specific naming patterns need to be deleted, combine with the -name parameter:

find ./my_dir -mtime +10 -name "*.script" -type f -delete

Avoiding Subdirectory Traversal

By default, the find command recursively searches all subdirectories. To process only the current directory, use:

find ./my_dir -maxdepth 1 -mtime +10 -type f -delete

Error Handling and Debugging

Common errors in practical applications include:

Adding error checking to scripts is recommended:

if [ -d "./my_dir" ]; then
    find ./my_dir -mtime +10 -type f -delete
else
    echo "Error: Directory does not exist"
    exit 1
fi

crontab Integration

Integrate cleanup scripts into crontab for automation:

# Execute cleanup every 10 days at 2 AM
0 2 */10 * * /path/to/cleanup_script.sh

Recording operation logs in scripts facilitates subsequent auditing:

#!/bin/bash
LOG_FILE="/var/log/file_cleanup.log"
DATE=$(date +"%Y-%m-%d %H:%M:%S")

echo "[$DATE] Starting file cleanup..." >> $LOG_FILE
find ./my_dir -mtime +10 -type f -delete -print >> $LOG_FILE 2>&1
echo "[$DATE] File cleanup completed" >> $LOG_FILE

Performance Optimization Recommendations

For directories containing large numbers of files, consider the following optimization measures:

Conclusion

Using the find command for time-based file cleanup is a fundamental skill in Unix/Linux system administration. By properly combining various parameters, efficient and secure automated file management can be achieved. The key is understanding the meaning of each parameter and conducting thorough testing in production environments. The methods introduced in this article are not only applicable to script file cleanup but can also be adapted for various file management tasks including log files, temporary files, and other file types.

Copyright Notice: All rights in this article are reserved by the operators of DevGex. Reasonable sharing and citation are welcome; any reproduction, excerpting, or re-publication without prior permission is prohibited.