Precise File Deletion by Hour Intervals Using find Command

Nov 23, 2025 · Programming · 16 views · 7.8

Keywords: find command | file deletion | bash scripting | time control | system administration

Abstract: This technical article explores precise file deletion methods in bash scripts using the find command. It provides a comprehensive analysis of the -mmin option for hour-level granularity, including parameter calculation, command syntax, and practical examples for deleting files older than 6 hours. The article also compares alternative tools like tmpwatch and tmpreaper, offering guidance for selecting optimal file cleanup strategies based on specific requirements.

Problem Context and Requirements

In system administration and automated script development, cleaning up expired temporary files or log files is a common task. While the traditional find command with the -mtime option supports day-level file deletion, finer time control—such as removing files older than 6 hours—requires more precise solutions.

Core Solution: Understanding the -mmin Option

The -mmin option in the find command enables minute-based calculation of file modification times, allowing for hour-level precision. This option accepts a numerical argument representing the number of minutes since the file was last modified.

The basic syntax is as follows:

find path -name filename_pattern -type f -mmin +minutes -delete

Practical Implementation Example

To delete all files matching the $REQUIRED_FILES pattern in the $LOCATION directory that were modified more than 6 hours ago, use the following command:

find $LOCATION -name $REQUIRED_FILES -type f -mmin +360 -delete

The key here is time calculation: 6 hours equals 360 minutes, so -mmin +360 finds files modified more than 360 minutes ago. The + symbol denotes "greater than," while - indicates "less than," and no symbol means "equal to."

Parameter Calculation and Considerations

When using -mmin, accurately calculate the target time in minutes:

It is advisable to use variables for time calculations in scripts to enhance readability and maintainability:

HOURS=6
MINUTES=$((HOURS * 60))
find $LOCATION -name $REQUIRED_FILES -type f -mmin +$MINUTES -delete

Comparison with Alternative Tools

Beyond the find command, other tools specialize in temporary file cleanup:

tmpwatch Tool

tmpwatch is designed specifically for removing unused temporary files and allows direct specification of hours:

tmpwatch 6 $LOCATION

tmpreaper Tool

tmpreaper offers similar temporary file cleaning capabilities with more intuitive syntax:

tmpreaper 6h $LOCATION

Best Practices and Recommendations

When selecting a file cleanup approach, consider the following factors:

Conclusion

The -mmin option in the find command provides highly precise control over file deletion timing, overcoming the day-level limitation of the traditional -mtime option. This method aligns with the Unix philosophy of tool reuse, delivering flexible and powerful file management capabilities.

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.