find with -exec for actions in Linux CLI - Time & Space Complexity
When using the find command with -exec, we want to know how the time to complete grows as we search more files.
We ask: How does running an action on each found file affect total time?
Analyze the time complexity of the following code snippet.
find /path/to/dir -type f -name "*.txt" -exec wc -l {} \;
This command finds all text files and runs wc -l to count lines in each file.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Running
wc -lon each found file. - How many times: Once for every file that matches the search.
As the number of matching files grows, the total work grows roughly the same way.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 10 times running wc -l |
| 100 | 100 times running wc -l |
| 1000 | 1000 times running wc -l |
Pattern observation: The total time grows directly with the number of files found.
Time Complexity: O(n)
This means the total time increases in a straight line as you add more files to process.
[X] Wrong: "The -exec action runs only once regardless of files found."
[OK] Correct: Actually, -exec runs once for each matching file, so time grows with the number of files.
Understanding how commands like find -exec scale helps you write scripts that handle many files efficiently and predict how long tasks will take.
What if we changed -exec to -exec ... + to run the action fewer times? How would the time complexity change?
Practice
-exec option do in the find command?Solution
Step 1: Understand the purpose of
The-exec-execoption infindruns a command on each file found by the search.Step 2: Differentiate from other options
It does not delete, list only directories, or change permissions by itself; it just runs the given command.Final Answer:
Runs a specified command on each file found byfind. -> Option BQuick Check:
-execruns commands on found files [OK]
-exec means "execute command" on files [OK]- Thinking
-execdeletes files automatically - Confusing
-execwith listing or filtering options - Forgetting to end
-execcommand with \;
.log files using find with -exec?Solution
Step 1: Identify correct
The-execending-execcommand must end with\;to mark the end of the command.Step 2: Check command correctness
find . -name '*.log' -exec rm {} \; correctly usesrm {}and ends with\;. Other options miss the backslash or semicolon or both.Final Answer:
find . -name '*.log' -exec rm {} \; -> Option CQuick Check:
Correct-execends with\;[OK]
-exec commands with \; [OK]- Omitting the backslash before semicolon
- Leaving out the semicolon entirely
- Not using {} as placeholder for file name
find /tmp -type f -name '*.txt' -exec echo File: {} \;Solution
Step 1: Understand the command
The command finds all files ending with .txt in /tmp and runsecho File: {}on each.Step 2: Predict output
For each file found, it prints "File: " followed by the full file path (because {} is replaced by the path).Final Answer:
Prints 'File: ' followed by each .txt file path found in /tmp. -> Option DQuick Check:
-exec echo File: {}prints file paths [OK]
- Thinking it deletes files
- Assuming only file names print without path
- Believing echo can't be used with -exec
find /var/log -name '*.log' -exec gzip {} ;Solution
Step 1: Check
The-execsyntax-execcommand must end with\;, not just;.Step 2: Validate other parts
Quotes around '*.log' are correct, andgzipis appropriate to compress logs.Final Answer:
Missing backslash before semicolon in-execending. -> Option AQuick Check:
-execmust end with\;[OK]
-exec [OK]- Using ; without backslash
- Confusing compression commands
- Removing quotes around patterns
/home/user and change their permissions to 644. Which command correctly uses find with -exec to do this?Solution
Step 1: Understand size filter and command
-size +5Mfinds files larger than 5MB.chmod 644 {}changes permissions of each found file.Step 2: Confirm correct
The command must end with-execsyntax\;to mark the end of the-execaction. find /home/user -size +5M -exec chmod 644 {} \; correctly includes this.Final Answer:
find /home/user -size +5M -exec chmod 644 {} \; -> Option AQuick Check:
Use-execwith {} and end with\;[OK]
-exec with \; [OK]- Omitting {} placeholder
- Missing backslash before semicolon
- Leaving out semicolon entirely
Ready to prove it?
Pass the test and this pattern moves unknown
