TechEarl

How to Find Files Modified in the Last 7 Days (find -mtime)

find -mtime -7 lists every file modified in the last 7 days. The catch is the off-by-one: -7 means less than 7 days ago, +7 means more than 7 days ago, and exact-7 almost never matches what people expect. The flag reference, worked variations for hours and minutes, the BSD vs GNU rounding difference, and the safe cleanup patterns.

Ishan KarunaratneIshan Karunaratne⏱️ 8 min readUpdated
Use find -mtime -7 to list files modified in the last 7 days. The off-by-one (-7 means under 7 days, +7 means over 7 days), -mmin for minute resolution, -newer for exact timestamps, and the BSD rounding gotcha on macOS.

find . -type f -mtime -7 lists every file modified in the last 7 days. The -mtime -7 reads as "modification time less than 7 days ago". Flip the sign to +7 and you get files older than 7 days, which is the version cleanup scripts use to clear out stale logs and temporary files.

The off-by-one is what catches people: -mtime 7 (no sign) means exactly 7 days ago, plus or minus rounding, which almost never matches what you actually want. Use -mtime -7 for recent, -mtime +7 for old, and never bare -mtime N unless you specifically want the 24-hour slice that landed N days ago.

Set your values

Try it with your own values

Set your OS, search path, and the number of days. Every find example below updates with your values.

The one-liner

bash· Linux (GNU)
find :search_path -type f -mtime -:days

That returns every regular file under your search path with a modification time newer than :days days ago. Drop -type f if you also want directories and symlinks.

What -mtime actually does (the off-by-one)

-mtime N filters by modification age in 24-hour units, rounded toward zero. The sign in front of N controls the comparison:

ExpressionMeaning
-mtime -7Modified in the last 7 days (age < 7)
-mtime +7Modified more than 7 days ago (age > 7)
-mtime 7Modified between 7 and 8 days ago (age == 7 after flooring)
-mtime -1Modified in the last 24 hours
-mtime 0Modified within the last 24 hours (same as -mtime -1)

The rounding rule: GNU find floors the file's age to whole 24-hour intervals before comparing. A file modified 6.9 days ago has age 6 and matches -mtime -7. A file modified 7.0001 days ago has age 7 and does not match -mtime -7. This is the off-by-one that breaks scripts when "last week" means slightly different things to different people.

If you need exact time windows, use -mmin (minute resolution) or -newer FILE (exact timestamp) instead.

Find files modified in the last N hours

-mmin -N is the minute-resolution variant. Use it when 24-hour buckets are too coarse.

bash· Linux (GNU)
find :search_path -type f -mmin -:minutes

For "modified in the last hour", use -mmin -60. For "modified in the last 15 minutes", -mmin -15. The same - / + sign rules apply.

Find files modified since a specific time

-newer FILE compares against the modification time of a reference file. The standard trick is to touch a marker file at the exact instant you care about, then ask find for everything newer:

bash· Linux (GNU)
touch -t 202605170900 /tmp/since-mark
find :search_path -type f -newer /tmp/since-mark

The touch -t YYYYMMDDhhmm syntax sets the modification time of the marker. This is more precise than -mtime because it avoids the day-floor rounding.

Find files modified between two dates

Combine -newer and ! -newer for a window:

bash· Linux (GNU)
touch -t 202605010000 /tmp/start-mark
touch -t 202605150000 /tmp/end-mark
find :search_path -type f -newer /tmp/start-mark ! -newer /tmp/end-mark

This returns files modified between May 1 and May 15 inclusive of the start, exclusive of the end. Standard practice for "give me everything from last week" reports.

Find files NOT modified in the last N days

The inverse, useful for finding stale files:

bash· Linux (GNU)
find :search_path -type f -mtime +:days

The + flips the comparison. -mtime +7 means "older than 7 days" which is exactly what cleanup scripts want.

Delete files older than N days

The classic cleanup pattern. Use -mtime +:days -delete to remove anything that hasn't been touched in the window:

bash· Linux (GNU)
find :search_path -type f -mtime +:days -delete

Critical safety habit: always run the command with -print first to verify what would be deleted, then swap -print for -delete. find -delete has no confirmation and no undo. The find and delete files article covers the safe-delete pattern in more depth, including how to handle the directory-deletion-order trap.

List the N most recently modified files

Combining find with sort and head gives you a recency leaderboard:

bash· Linux (GNU)
find :search_path -type f -printf '%T@ %p\n' | sort -rn | head -20

GNU find -printf '%T@' outputs the modification time as a Unix timestamp; BSD lacks -printf, so use -exec stat. The numeric sort surfaces the most recent first; head -20 keeps the output scannable.

macOS BSD vs GNU find

The mtime-specific differences that bite:

BehaviorGNU findBSD find (macOS default)
-mtime -N roundingFloors to 24-hour unitsDifferent rounding edge cases
-mmin (minute resolution)SupportedSupported
-newer FILESupportedSupported
-newerXY (precise comparison)Supported (rare)Supported, more variants
-printf '%T@' (epoch time)SupportedNOT supported; use -exec stat -f '%m %N'
-anewer (accessed since FILE)SupportedSupported

If your cleanup scripts need to run on both Linux and macOS, stick to -mtime, -mmin, -newer. Avoid -printf and any GNU-specific format flags.

Common find -mtime mistakes

1. Using -mtime N instead of -mtime -N or +N. Bare -mtime 7 means "exactly 7 days old after rounding" which matches files modified somewhere in the 24-hour window 7-8 days ago. Almost never what you want. Always include - (recent) or + (old).

2. Forgetting that -mtime -1 is the same as -mtime 0. Both match "modified in the last 24 hours". The negative-zero edge case trips up scripts that try to detect "newer than yesterday" using arithmetic.

3. Assuming -mtime uses calendar days. It uses 24-hour intervals from the current moment, not midnight-to-midnight boundaries. A file modified yesterday at 11:00 PM and checked at 6:00 AM today has age < 1 day and matches -mtime -1, not because it was "modified yesterday" but because it was modified less than 24 hours ago.

4. Mixing -mtime and -newer thinking they're equivalent. -newer FILE compares against the file's exact mtime; -mtime N compares against the floor-to-day age. For scripts that need precision, prefer -newer.

5. Forgetting -type f. Without it, -mtime -7 -delete will try to delete directories whose mtime is recent, which is almost any directory you've added files to. Always include -type f for file-only operations, or -type d -empty for directory cleanup.

6. Running cleanup against /. find / -mtime +30 -delete is a foot-cannon. Always anchor the search at a specific directory. The find and delete files article has the full safe-delete checklist.

When NOT to use -mtime

Use a different approach when:

  • You need sub-minute resolution. -mmin is whole-minute only. For sub-minute precision, use -newer FILE with a touch-set marker file.
  • You're working with file content changes, not metadata. -mtime reflects when the inode's mtime was updated, which any write triggers. If you care about "files whose content actually differs from yesterday's snapshot", you want a content hash diff, not find.
  • You need to track access patterns, not modifications. -atime is the access-time variant; on many filesystems it's disabled (noatime mount option) for performance, so this often returns the same as -mtime.
  • You're on a filesystem where mtime is unreliable. Some network filesystems, FUSE mounts, and badly-configured rsync setups update mtime in ways that confuse -mtime. Verify with stat before trusting the find output.

See also

FAQ

TagsfindmtimeCLILinuxmacOSBSDCleanup Scripts
Share
Ishan Karunaratne

Ishan Karunaratne

Tech Architect · Software Engineer · AI/DevOps

Tech architect and software engineer with 20+ years across software, Linux systems, DevOps, and infrastructure — and a more recent focus on AI. Currently Chief Technology Officer at a tech startup in the healthcare space.

Keep reading

Related posts

Use find -size +100M to list files larger than 100 megabytes. Unit suffixes (c/k/M/G), +/- sign convention, combine with sort -rn to surface the biggest files on disk, and BSD vs GNU rendering differences.

How to Find Files Larger Than a Size with find -size

find . -size +100M lists every file larger than 100 megabytes. The unit suffixes (c, k, M, G), the +/- sign convention, how to combine with sort to find the biggest files on disk, the BSD vs GNU divergence for printing sizes, and the wc -c trick for byte-exact thresholds.

Use find ... -print0 | xargs -0 grep -l 'PATTERN' to find every file containing a string. When grep -r is enough, when to add find as a pre-filter for performance, multi-pattern matching with -E, and the safe NUL-delimited pipeline.

How to Find Files Containing Specific Text (find + grep)

find ... -print0 | xargs -0 grep -l 'PATTERN' finds every file containing a piece of text. The combo handles weird filenames, scales to huge trees, and replaces three other common but broken pipelines. When to use grep -r alone, when to add find as a pre-filter, and the BSD vs GNU pitfalls.