What if you could find and fix thousands of files with a single command?
Why find with -exec for actions in Linux CLI? - Purpose & Use Cases
Imagine you have thousands of files scattered across many folders on your computer. You need to find all files with a certain name or type and then delete or move them one by one.
Manually searching through folders and opening each file to decide what to do is slow and tiring. It's easy to miss files or make mistakes, especially when dealing with many files.
The find command with -exec lets you search for files and immediately perform actions on them automatically. This saves time and avoids errors by combining search and action in one step.
ls | grep '.log'
rm file1.log
rm file2.log
rm file3.logfind . -name '*.log' -exec rm {} \;You can quickly and safely find files and run any command on them, automating repetitive tasks with ease.
Cleaning up old backup files by finding all .bak files and deleting them automatically without opening each folder.
Manual file handling is slow and error-prone.
find -exec combines searching and acting in one command.
This makes file management faster, safer, and more efficient.
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
