find by modification time in Linux CLI - Time & Space Complexity
When using the find command to search files by modification time, it is important to understand how the time to complete the search grows as the number of files increases.
We want to know how the command's work changes when there are more files to check.
Analyze the time complexity of the following code snippet.
find /path/to/dir -type f -mtime -7
This command finds all files in the given directory modified within the last 7 days.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Checking each file's metadata (modification time).
- How many times: Once for every file in the directory and its subdirectories.
As the number of files grows, the command must check more files, so the work grows proportionally.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | About 10 file checks |
| 100 | About 100 file checks |
| 1000 | About 1000 file checks |
Pattern observation: The number of operations grows roughly in direct proportion to the number of files.
Time Complexity: O(n)
This means the time to complete the search grows linearly with the number of files to check.
[X] Wrong: "The command only checks a few files because it stops early when it finds matches."
[OK] Correct: The find command checks every file to be sure it finds all matches; it does not stop early.
Understanding how file search commands scale helps you reason about script performance and system load, a useful skill in many automation tasks.
"What if we added a condition to search only in the top directory without recursion? How would the time complexity change?"
Practice
find . -mtime -3 do in Linux?Solution
Step 1: Understand the meaning of -mtime
The-mtimeoption infindsearches files by modification time in days.Step 2: Interpret the negative number -3
A negative number means files modified less than that many days ago, so-3means within the last 3 days.Final Answer:
Finds files modified within the last 3 days -> Option AQuick Check:
-mtime -3means modified less than 3 days ago [OK]
- Confusing -mtime -3 with exactly 3 days ago
- Thinking -mtime -3 means more than 3 days ago
- Mixing hours with days in -mtime
Solution
Step 1: Recall the meaning of -mtime with positive number
-mtime 5finds files modified exactly 5 days ago (between 5 and 6 days).Step 2: Check other options
-mtime +5means more than 5 days ago,-mtime -5means less than 5 days ago, and-mmin 5searches by minutes, not days.Final Answer:
find . -mtime 5 -> Option BQuick Check:
Exact days use positive number without + or - [OK]
- Using +5 instead of 5 for exact days
- Using -5 to mean exactly 5 days ago
- Confusing -mtime with -mmin
find . -mtime +4Solution
Step 1: Understand +4 with -mtime
-mtime +4finds files modified more than 4 days ago (5 days or older).Step 2: Identify files matching condition
Files modified 5 and 7 days ago match, so they will be listed.Final Answer:
./file_modified_5_days_ago ./file_modified_7_days_ago -> Option DQuick Check:
+4 means older than 4 days [OK]
- Including files modified less than or equal to 4 days ago
- Confusing +4 with -4
- Expecting no output when files exist
find /data -mtime 2But it returns files modified exactly 2 days ago, not within 2 days. How to fix it?
Solution
Step 1: Understand what -mtime 2 means
-mtime 2finds files modified exactly 2 days ago (between 2 and 3 days).Step 2: Use negative number for 'within last N days'
To find files modified less than 2 days ago, use-mtime -2.Final Answer:
Change to find /data -mtime -2 -> Option CQuick Check:
Negative -mtime means less than N days [OK]
- Using positive number for 'within' instead of exact days
- Using +2 which means older than 2 days
- Confusing minutes and days options
/backup that were modified more than 10 days ago but keep files modified within 10 days. Which command correctly does this safely?Solution
Step 1: Identify files older than 10 days
-mtime +10selects files modified more than 10 days ago.Step 2: Use safe deletion option
-deletedeletes files directly; it's safer and simpler than-exec rmwhich can be error-prone.Step 3: Check other options
-mtime -10deletes recent files (wrong),-mtime 10deletes files exactly 10 days old (not older), and-exec rmworks but less safe than-delete.Final Answer:
find /backup -mtime +10 -delete -> Option AQuick Check:
Use +N with -delete to remove files older than N days [OK]
- 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
Ready to prove it?
Pass the test and this pattern moves unknown
