Linux find -mtime: Find and Delete Files by Age.

Learn how to use Linux find -mtime to locate files by modification age. Understand the differences between +N and -N, preview matching files, and safely remove outdated files with find and rm.

Linux find -mtime: Find and Delete Files by Age.
Photo by yannick Coffi —see +< @ yannickcoffi @ PRINTS

The Linux find command allows you to locate files based on their modification time, making it useful for system maintenance, log rotation, and disk space management.

One particularly useful option is -mtime, which measures file age in 24-hour periods. Understanding how its +N and -N operators work is essential when automating file cleanup.

Let's look at how to use it safely.

1. Understanding -mtime

The -mtime option evaluates the time elapsed since a file was last modified, counting complete 24-hour periods.

ExpressionMeaning
-mtime -3Modified less than 72 hours ago
-mtime 3Modified at least 72 hours ago but less than 96 hours ago
-mtime +3Modified at least 96 hours ago

Important: -mtime +3 does not mean strictly more than 72 hours ago. Because find rounds down the elapsed time to whole 24-hour periods, it matches files that are at least four complete days old.

2. Find Files Modified More Than Three Days Ago

To locate files that have not been modified for at least 96 hours:

$ find /path/to/dir -type f -mtime +3 -print

Here:

  • -type f matches regular files only.
  • -mtime +3 matches files whose modification age is at least four complete 24-hour periods.
  • -print displays the matching file paths without deleting anything.

Delete the matching files

Once you have verified the results, you can remove the files:

$ find /path/to/dir -type f -mtime +3 -exec rm -f -- {} +

The -exec option passes matching file paths to rm, which removes them. The + terminator allows multiple paths to be processed in batches.

Warning: This operation permanently removes matching files.
Verify the target directory and preview the results before executing it.

3. Find Files Modified Within the Last Three Days

To locate files modified less than 72 hours ago:

$ find /path/to/dir -type f -mtime -3 -print

This matches files modified within the preceding 72 hours, including recently modified files from the current day.

To delete those files instead:

$ find /path/to/dir -type f -mtime -3 -exec rm -f -- {} +

Be particularly careful with this expression: it targets recent files, not old ones.

4. Preview Before Deleting

Before using rm, always check which files match your criteria.

For example, to list files at least four days old:

$ find /path/to/dir -type f -mtime +3 -print

Review the output carefully before running the deletion command.

Using -print is preferable to -exec ls -l {} \; for this purpose because it avoids unnecessary command execution and handles filenames more predictably.

5. Need More Precise Time Filtering?

If you need to filter files by hours or minutes rather than complete 24-hour periods, use -mmin.

For example, to find files modified more than 60 minutes ago:

$ find /path/to/dir -type f -mmin +60 -print

To find files modified within the last 60 minutes:

$ find /path/to/dir -type f -mmin -60 -print

These expressions are useful for more precise cleanup tasks and automated maintenance scripts.


The find -mtime option is a practical tool for managing files based on their modification age.

Remember:

  • -mtime +3 matches files at least 96 hours old.
  • -mtime -3 matches files modified less than 72 hours ago.
  • -mtime 3 matches files between 72 and 96 hours old.

Always preview matching files before deleting them, and use -type f when your intention is to target regular files rather than directories.

We hope you found these tips useful.

Keep Us Caffeinated  ⦿ ⦿
Spotify Logo
↑
SNUBmonkey Join our readers