Bird
Raised Fist0
Linux CLIscripting~15 mins

find by modification time in Linux CLI - Deep Dive

Choose your learning style10 modes available
Overview - find by modification time
What is it?
The 'find' command in Linux lets you search for files and folders based on many criteria. One useful way is by modification time, which means when a file was last changed. You can find files modified within a certain number of days or minutes. This helps you quickly locate recent or old files without opening each one.
Why it matters
Without the ability to find files by modification time, managing files would be slow and error-prone. Imagine trying to clean up old files or back up recent work without knowing when files changed. This command saves time and reduces mistakes by automating file searches based on time.
Where it fits
Before learning this, you should know basic Linux commands and how to navigate the file system. After mastering this, you can combine 'find' with other commands to automate backups, cleanups, or reports based on file age.
Mental Model
Core Idea
Finding files by modification time means telling the system to look for files changed within a specific time window.
Think of it like...
It's like sorting your mail by the date it arrived, so you can quickly see the newest letters or the oldest ones you haven't opened.
┌───────────────┐
│   find root   │
├───────────────┤
│ -mtime n      │  <-- files modified n days ago
│ -mmin n       │  <-- files modified n minutes ago
│ -newer file   │  <-- files newer than a reference file
└───────────────┘
Build-Up - 7 Steps
1
FoundationBasic find command usage
🤔
Concept: Learn how to use the 'find' command to search files by name and location.
The 'find' command searches directories recursively. For example, 'find /tmp -name "*.txt"' finds all text files in /tmp and its subfolders.
Result
/tmp/file1.txt /tmp/docs/note.txt
Understanding the basic 'find' command is essential before adding time-based filters.
2
FoundationUnderstanding file modification time
🤔
Concept: Know what modification time means and how the system tracks it.
Every file has metadata including modification time (mtime), which updates when the file content changes. You can see it with 'ls -l' or 'stat filename'.
Result
Modify time: 2024-06-01 14:30:00
Knowing what modification time is helps you understand why 'find' can filter files by it.
3
IntermediateUsing -mtime to find files by days
🤔Before reading on: do you think '-mtime 1' finds files modified exactly 1 day ago or within the last 1 day? Commit to your answer.
Concept: Use '-mtime' to find files modified a certain number of days ago, counting full 24-hour periods.
Command: find /tmp -mtime 1 This finds files modified between 24 and 48 hours ago. Use '-mtime -1' for files modified less than 1 day ago.
Result
/tmp/recentfile.txt
Understanding how '-mtime' counts days as full 24-hour blocks avoids confusion about which files match.
4
IntermediateUsing -mmin to find files by minutes
🤔Before reading on: does '-mmin -30' find files modified more or less than 30 minutes ago? Commit to your answer.
Concept: Use '-mmin' to find files modified within a number of minutes, allowing finer time control.
Command: find /tmp -mmin -30 Finds files modified in the last 30 minutes. Negative means less than, positive means exactly that many minutes ago.
Result
/tmp/very_recent.txt
Knowing '-mmin' lets you find files with minute precision, useful for very recent changes.
5
IntermediateUsing -newer to compare file times
🤔
Concept: Find files newer than a reference file by comparing modification times.
Command: find /tmp -newer reference.txt Finds files modified more recently than 'reference.txt'. This lets you use any file as a time marker.
Result
/tmp/updated_after_reference.log
Using a file as a time reference is flexible and powerful for custom time comparisons.
6
AdvancedCombining time filters with actions
🤔Before reading on: do you think 'find -mtime +7 -exec rm {} \;' deletes files modified more or less than 7 days ago? Commit to your answer.
Concept: Combine time filters with commands like '-exec' to automate actions on matched files.
Command: find /tmp -mtime +7 -exec rm {} \; Deletes files older than 7 days. The '+7' means more than 7 days ago.
Result
Old files removed from /tmp
Combining filters with actions automates maintenance tasks safely and efficiently.
7
ExpertUnderstanding time granularity and edge cases
🤔Before reading on: does 'find' consider file modification time in seconds or only days/minutes? Commit to your answer.
Concept: Explore how 'find' handles time internally, including rounding and filesystem timestamp precision.
The '-mtime' option counts full 24-hour periods rounded down. '-mmin' counts minutes but may round seconds. Filesystem timestamps may have limited precision (e.g., 1 second). This can cause unexpected matches near boundaries.
Result
Some files near time limits may or may not appear depending on exact timestamps.
Knowing time rounding and precision prevents subtle bugs in time-based file searches.
Under the Hood
The 'find' command reads each file's metadata from the filesystem, specifically the modification time stored as a timestamp. It compares this timestamp against the current time minus the specified offset (days or minutes). For '-newer', it compares timestamps between files. The command uses system calls to access this metadata efficiently during directory traversal.
Why designed this way?
This design leverages existing filesystem metadata without extra overhead. Using timestamps allows fast comparisons and flexible queries. Alternatives like scanning file contents would be slow. The choice of days and minutes as units balances usability and precision for most tasks.
┌───────────────┐
│  find command │
└──────┬────────┘
       │
       ▼
┌───────────────┐
│ Directory     │
│ traversal     │
└──────┬────────┘
       │
       ▼
┌───────────────┐
│ Read file     │
│ metadata      │
└──────┬────────┘
       │
       ▼
┌───────────────┐
│ Compare mtime │
│ with criteria │
└──────┬────────┘
       │
       ▼
┌───────────────┐
│ Match?        │
│ Yes → output  │
│ No → skip     │
└───────────────┘
Myth Busters - 4 Common Misconceptions
Quick: Does '-mtime 1' find files modified exactly 1 day ago or within the last 1 day? Commit to your answer.
Common Belief:People often think '-mtime 1' finds files modified within the last 24 hours.
Tap to reveal reality
Reality:'-mtime 1' finds files modified between 24 and 48 hours ago, not within the last day. To find files modified less than 1 day ago, use '-mtime -1'.
Why it matters:Misunderstanding this causes missing or extra files in searches, leading to wrong backups or deletions.
Quick: Does '-mmin -30' find files modified more or less than 30 minutes ago? Commit to your answer.
Common Belief:Many believe '-mmin -30' finds files modified more than 30 minutes ago.
Tap to reveal reality
Reality:It actually finds files modified less than 30 minutes ago (i.e., recently).
Why it matters:Confusing this reverses the intended search, causing wrong files to be processed.
Quick: Does 'find -newer file1' find files older or newer than file1? Commit to your answer.
Common Belief:Some think it finds files older than the reference file.
Tap to reveal reality
Reality:It finds files modified more recently (newer) than the reference file.
Why it matters:Using the wrong comparison can cause missing important files or deleting recent data.
Quick: Does 'find' consider seconds in '-mtime' or only full days? Commit to your answer.
Common Belief:People often believe '-mtime' uses exact seconds for matching.
Tap to reveal reality
Reality:'-mtime' counts full 24-hour periods rounded down, ignoring hours, minutes, and seconds.
Why it matters:This rounding can cause unexpected matches near day boundaries, confusing users.
Expert Zone
1
The '-mtime' option counts time in 24-hour blocks from the current time, not calendar days, which can confuse users around midnight.
2
Using '-newer' with a temporary timestamp file allows complex time comparisons without hardcoding dates.
3
Filesystem timestamp precision varies; some filesystems only store modification times to the nearest second or even coarser, affecting find accuracy.
When NOT to use
Avoid using 'find' with modification time for very large filesystems where performance is critical; specialized indexing tools like 'locate' or 'mlocate' are faster. Also, for real-time monitoring, use 'inotify' instead of periodic 'find' scans.
Production Patterns
In production, 'find' with '-mtime' is used in automated cleanup scripts to delete old logs, backup scripts to archive recent files, and monitoring tools to alert on stale files. Combining with '-exec' or piping to 'xargs' enables powerful batch operations.
Connections
Cron jobs
Builds-on
Knowing how to find files by modification time helps schedule automated tasks in cron that act on files based on age.
Version control systems
Related pattern
Both track changes over time, but version control stores history explicitly, while 'find' uses filesystem timestamps to infer recency.
Digital forensics
Similar principle
Forensics experts analyze file modification times to reconstruct events, showing how time-based file data is critical beyond scripting.
Common Pitfalls
#1Using '-mtime 1' expecting files modified within the last day.
Wrong approach:find /tmp -mtime 1
Correct approach:find /tmp -mtime -1
Root cause:Misunderstanding that '-mtime n' means exactly n days ago, not less than n days.
#2Confusing '-mmin' sign usage and finding files older than intended.
Wrong approach:find /tmp -mmin 30
Correct approach:find /tmp -mmin +30
Root cause:Not knowing that positive means exactly n minutes ago, '+' means more than n minutes, and '-' means less than n minutes.
#3Using '-newer' without a proper reference file.
Wrong approach:find /tmp -newer nonexistent.file
Correct approach:touch reference.file find /tmp -newer reference.file
Root cause:Forgetting to create or specify a valid reference file for comparison.
Key Takeaways
The 'find' command can filter files by modification time using '-mtime' for days and '-mmin' for minutes.
Understanding how time arguments count full days or minutes prevents common mistakes in file selection.
Using '-newer' lets you compare files against a reference file's modification time for flexible searches.
Combining time filters with actions like '-exec' automates file management tasks efficiently.
Knowing filesystem timestamp precision and rounding behavior helps avoid subtle bugs in time-based searches.

Practice

(1/5)
1. What does the command find . -mtime -3 do in Linux?
easy
A. Finds files modified within the last 3 days
B. Finds files modified exactly 3 days ago
C. Finds files modified more than 3 days ago
D. Finds files modified more than 3 hours ago

Solution

  1. Step 1: Understand the meaning of -mtime

    The -mtime option in find searches files by modification time in days.
  2. Step 2: Interpret the negative number -3

    A negative number means files modified less than that many days ago, so -3 means within the last 3 days.
  3. Final Answer:

    Finds files modified within the last 3 days -> Option A
  4. Quick Check:

    -mtime -3 means modified less than 3 days ago [OK]
Hint: Negative -mtime means files modified within last N days [OK]
Common Mistakes:
  • Confusing -mtime -3 with exactly 3 days ago
  • Thinking -mtime -3 means more than 3 days ago
  • Mixing hours with days in -mtime
2. Which of the following is the correct syntax to find files modified exactly 5 days ago?
easy
A. find . -mtime +5
B. find . -mtime 5
C. find . -mtime -5
D. find . -mmin 5

Solution

  1. Step 1: Recall the meaning of -mtime with positive number

    -mtime 5 finds files modified exactly 5 days ago (between 5 and 6 days).
  2. Step 2: Check other options

    -mtime +5 means more than 5 days ago, -mtime -5 means less than 5 days ago, and -mmin 5 searches by minutes, not days.
  3. Final Answer:

    find . -mtime 5 -> Option B
  4. Quick Check:

    Exact days use positive number without + or - [OK]
Hint: Exact days use plain number with -mtime (no + or -) [OK]
Common Mistakes:
  • Using +5 instead of 5 for exact days
  • Using -5 to mean exactly 5 days ago
  • Confusing -mtime with -mmin
3. What will be the output of this command if run in a directory with files modified 1, 3, 5, and 7 days ago?
find . -mtime +4
medium
A. ./file_modified_1_day_ago ./file_modified_3_days_ago
B. ./file_modified_3_days_ago ./file_modified_5_days_ago
C. No files found
D. ./file_modified_5_days_ago ./file_modified_7_days_ago

Solution

  1. Step 1: Understand +4 with -mtime

    -mtime +4 finds files modified more than 4 days ago (5 days or older).
  2. Step 2: Identify files matching condition

    Files modified 5 and 7 days ago match, so they will be listed.
  3. Final Answer:

    ./file_modified_5_days_ago ./file_modified_7_days_ago -> Option D
  4. Quick Check:

    +4 means older than 4 days [OK]
Hint: Use +N to find files older than N days [OK]
Common Mistakes:
  • Including files modified less than or equal to 4 days ago
  • Confusing +4 with -4
  • Expecting no output when files exist
4. You wrote this command to find files modified within the last 2 days:
find /data -mtime 2
But it returns files modified exactly 2 days ago, not within 2 days. How to fix it?
medium
A. Use find /data -mtime 0
B. Change to find /data -mtime +2
C. Change to find /data -mtime -2
D. Add -mmin -2880 instead

Solution

  1. Step 1: Understand what -mtime 2 means

    -mtime 2 finds files modified exactly 2 days ago (between 2 and 3 days).
  2. Step 2: Use negative number for 'within last N days'

    To find files modified less than 2 days ago, use -mtime -2.
  3. Final Answer:

    Change to find /data -mtime -2 -> Option C
  4. Quick Check:

    Negative -mtime means less than N days [OK]
Hint: Use -mtime -N for files modified within last N days [OK]
Common Mistakes:
  • Using positive number for 'within' instead of exact days
  • Using +2 which means older than 2 days
  • Confusing minutes and days options
5. You want to delete files in /backup that were modified more than 10 days ago but keep files modified within 10 days. Which command correctly does this safely?
hard
A. find /backup -mtime +10 -delete
B. find /backup -mtime +10 -exec rm {} \;
C. find /backup -mtime 10 -delete
D. find /backup -mtime -10 -exec rm {} \;

Solution

  1. Step 1: Identify files older than 10 days

    -mtime +10 selects files modified more than 10 days ago.
  2. Step 2: Use safe deletion option

    -delete deletes files directly; it's safer and simpler than -exec rm which can be error-prone.
  3. Step 3: Check other options

    -mtime -10 deletes recent files (wrong), -mtime 10 deletes files exactly 10 days old (not older), and -exec rm works but less safe than -delete.
  4. Final Answer:

    find /backup -mtime +10 -delete -> Option A
  5. Quick Check:

    Use +N with -delete to remove files older than N days [OK]
Hint: Use -mtime +N with -delete to remove files older than N days [OK]
Common Mistakes:
  • Using -mtime -10 deletes recent files instead of old
  • Using -mtime 10 deletes only files exactly 10 days old
  • Using -exec rm without care can cause errors
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard