📖 Scenario: You are managing files in a folder on your Linux system. You want to find certain files and perform actions on them automatically.
🎯 Goal: Learn how to use the find command with the -exec option to search for files and run commands on each found file.
📋 What You'll Learn
Use the find command to locate files
Use the -exec option to run commands on found files
Understand how to use placeholders like {} in -exec
Print the results of the commands run on the files
💡 Why This Matters
🌍 Real World
System administrators often need to find files and run commands on them automatically, like cleaning up logs or checking file contents.
💼 Career
Knowing how to use find with -exec is a key skill for Linux system management and automation tasks.
Progress0 / 4 steps
1
Create a test directory with files
Create a directory called test_dir and inside it create three empty files named file1.txt, file2.log, and file3.txt.
Linux CLI
Hint
Use mkdir to make a directory and touch to create empty files.
2
Set up a command to count lines
Create a variable called count_cmd and set it to the command wc -l which counts lines in a file.
Linux CLI
Hint
Assign the string 'wc -l' to the variable count_cmd.
3
Use find with -exec to count lines in .txt files
Use the find command to search in test_dir for files ending with .txt and use -exec to run the command stored in count_cmd on each file. Use {} as a placeholder for the filename and end the -exec command with \;.
Linux CLI
Hint
Use find test_dir -name '*.txt' -exec $count_cmd {} \; to run the command on each .txt file.
4
Display the output of the find command
Run the find command with -exec as before and print its output to see the line counts for each .txt file.
Linux CLI
Hint
The output should show zero lines for each .txt file since they are empty.
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.