What does the -exec option do in the find command?
It allows you to run a command on each file that find locates. This means you can perform actions like deleting, moving, or printing files directly from find.
Click to reveal answer
beginner
How do you correctly end a command used with -exec in find?
You end the command with \;. This tells find where the command finishes.
Click to reveal answer
beginner
What does {} represent in the find -exec command?
It is a placeholder for the current file found by find. The command replaces {} with the file name.
Click to reveal answer
intermediate
Why might you use find . -name '*.txt' -exec rm {} \;?
This command finds all files ending with .txt in the current directory and deletes each one by running rm on them.
Click to reveal answer
intermediate
What is the difference between -exec command {} \; and -exec command {} + in find?
Using \; runs the command once per file. Using + runs the command once with all files as arguments, which can be faster.
Click to reveal answer
What symbol ends the command in find -exec?
A;
B\;
C{}
D+
✗ Incorrect
The command must end with a backslash and semicolon (\;) to tell find where the command ends.
In find . -exec ls -l {} \;, what does {} do?
AIs replaced by the found file name
BRuns the command in background
CEnds the command
DRepresents the current directory
✗ Incorrect
{} is replaced by each file found by find.
Which command deletes all .log files in the current directory and subdirectories?
Arm *.log
Bfind . -name '*.log' -print
Cfind . -name '*.log' -exec rm {} \;
Dls *.log -exec rm {} \;
✗ Incorrect
Option C uses find with -exec rm to delete each .log file found.
What is the benefit of using -exec command {} + over -exec command {} \;?
ARuns command once per file
BDeletes files faster
CPrints file names only
DRuns command once with all files as arguments
✗ Incorrect
Using + runs the command once with all files, which is more efficient.
Which of these is a correct way to move all .txt files to a folder named backup?
Afind . -name '*.txt' -exec mv {} backup \;
Bfind . -name '*.txt' -exec mv backup {} \;
Cmv *.txt backup
Dfind . -exec mv {} backup \;
✗ Incorrect
Option A correctly uses find with -exec mv to move each .txt file to backup.
Explain how the -exec option works in the find command and how to use it safely.
Think about how you tell find what to do with each file it finds.
You got /4 concepts.
Describe the difference between ending -exec with \; and + and when to use each.
Consider performance and command compatibility.
You got /4 concepts.
Practice
(1/5)
1. What does the -exec option do in the find command?
easy
A. Changes the permissions of files found automatically.
B. Runs a specified command on each file found by find.
C. Lists only directories found by find.
D. Deletes all files found without confirmation.
Solution
Step 1: Understand the purpose of -exec
The -exec option in find runs 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 by find. -> Option B
Quick Check:
-exec runs commands on found files [OK]
Hint: Remember: -exec means "execute command" on files [OK]
Common Mistakes:
Thinking -exec deletes files automatically
Confusing -exec with listing or filtering options
Forgetting to end -exec command with \;
2. Which of the following is the correct syntax to delete all .log files using find with -exec?
easy
A. find . -name '*.log' -exec rm {}
B. find . -name '*.log' -exec rm {} ;
C. find . -name '*.log' -exec rm {} \;
D. find . -name '*.log' -exec rm \;
Solution
Step 1: Identify correct -exec ending
The -exec command must end with \; to mark the end of the command.
Step 2: Check command correctness
find . -name '*.log' -exec rm {} \; correctly uses rm {} and ends with \;. Other options miss the backslash or semicolon or both.
B. Shows an error because echo cannot be used with -exec.
C. Lists only the names of .txt files without path.
D. Prints 'File: ' followed by each .txt file path found in /tmp.
Solution
Step 1: Understand the command
The command finds all files ending with .txt in /tmp and runs echo 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 D
Quick Check:
-exec echo File: {} prints file paths [OK]
Hint: Remember {} is replaced by each file path found [OK]
Common Mistakes:
Thinking it deletes files
Assuming only file names print without path
Believing echo can't be used with -exec
4. Identify the error in this command:
find /var/log -name '*.log' -exec gzip {} ;
medium
A. Missing backslash before semicolon in -exec ending.
B. Incorrect use of quotes around '*.log'.
C. Using gzip instead of gunzip.
D. No error; command is correct.
Solution
Step 1: Check -exec syntax
The -exec command must end with \;, not just ;.
Step 2: Validate other parts
Quotes around '*.log' are correct, and gzip is appropriate to compress logs.
Final Answer:
Missing backslash before semicolon in -exec ending. -> Option A
Quick Check:
-exec must end with \; [OK]
Hint: Check for \ before ; to end -exec [OK]
Common Mistakes:
Using ; without backslash
Confusing compression commands
Removing quotes around patterns
5. You want to find all files larger than 5MB in /home/user and change their permissions to 644. Which command correctly uses find with -exec to do this?
hard
A. find /home/user -size +5M -exec chmod 644 {} \;
B. find /home/user -size +5M -exec chmod 644 {} ;
C. find /home/user -size +5M -exec chmod 644 \;
D. find /home/user -size +5M -exec chmod 644 {}
Solution
Step 1: Understand size filter and command
-size +5M finds files larger than 5MB. chmod 644 {} changes permissions of each found file.
Step 2: Confirm correct -exec syntax
The command must end with \; to mark the end of the -exec action. find /home/user -size +5M -exec chmod 644 {} \; correctly includes this.