Bird
Raised Fist0
Linux CLIscripting~5 mins

find with -exec for actions in Linux CLI - Commands & Configuration

Choose your learning style10 modes available
Introduction
Sometimes you want to find files and then do something with each file you find. The find command with the -exec option lets you run another command on every file it finds. This helps automate tasks like deleting, moving, or changing files all at once.
When you want to delete all log files older than 7 days in a folder.
When you need to change permissions on all scripts in a directory.
When you want to move all image files from one folder to another.
When you want to list details of all files with a certain extension.
When you want to compress all text files in a directory automatically.
Commands
This command finds all files ending with .log in /tmp and deletes each one. The {} is replaced by each file name, and \; ends the -exec command.
Terminal
find /tmp -name '*.log' -exec rm {} \;
Expected OutputExpected
No output (command runs silently)
→
-name '*.log' - Selects files with .log extension
→
-exec - Runs a command on each found file
This finds all regular files in /tmp/scripts and makes them executable by adding the +x permission.
Terminal
find /tmp/scripts -type f -exec chmod +x {} \;
Expected OutputExpected
No output (command runs silently)
→
-type f - Limits search to regular files
→
-exec - Runs chmod +x on each file
This finds all .jpg files in /tmp/images and moves them to /tmp/photos directory.
Terminal
find /tmp/images -name '*.jpg' -exec mv {} /tmp/photos/ \;
Expected OutputExpected
No output (command runs silently)
→
-name '*.jpg' - Selects JPEG image files
→
-exec - Runs mv command on each file
This finds all .txt files in /tmp/docs and lists their details using ls -l.
Terminal
find /tmp/docs -name '*.txt' -exec ls -l {} \;
Expected OutputExpected
-rw-r--r-- 1 user user 1234 Apr 26 10:00 /tmp/docs/file1.txt -rw-r--r-- 1 user user 5678 Apr 26 10:05 /tmp/docs/file2.txt
→
-name '*.txt' - Selects text files
→
-exec - Runs ls -l on each file
Key Concept

If you remember nothing else from this pattern, remember: the -exec option lets you run any command on each file found by find, using {} as a placeholder for the file name.

Common Mistakes
Forgetting to escape the semicolon at the end of the -exec command (writing -exec rm {} ; instead of -exec rm {} \;)
The shell interprets the semicolon early, so find does not get the full command and fails.
Always escape the semicolon with a backslash like \; or quote it ';' to end the -exec command properly.
Using {} without spaces around it in the -exec command (like -exec rm{} \;)
The command will not recognize the file name properly and will fail.
Always put spaces before and after {} so the command receives the file name correctly.
Trying to run multiple commands in one -exec without proper syntax.
find expects one command per -exec; multiple commands need to be combined properly or use -exec with sh -c.
Use -exec sh -c 'command1; command2' {} \; or run separate find commands.
Summary
Use find with -exec to run commands on each file found automatically.
Remember to use {} as a placeholder for the file name and end with \; to close the command.
Common uses include deleting, moving, changing permissions, or listing files.

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

  1. Step 1: Understand the purpose of -exec

    The -exec option in find runs a command on each file found by the search.
  2. Step 2: Differentiate from other options

    It does not delete, list only directories, or change permissions by itself; it just runs the given command.
  3. Final Answer:

    Runs a specified command on each file found by find. -> Option B
  4. 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

  1. Step 1: Identify correct -exec ending

    The -exec command must end with \; to mark the end of the command.
  2. 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.
  3. Final Answer:

    find . -name '*.log' -exec rm {} \; -> Option C
  4. Quick Check:

    Correct -exec ends with \; [OK]
Hint: Always end -exec commands with \; [OK]
Common Mistakes:
  • Omitting the backslash before semicolon
  • Leaving out the semicolon entirely
  • Not using {} as placeholder for file name
3. What will be the output of this command?
find /tmp -type f -name '*.txt' -exec echo File: {} \;
medium
A. Deletes all .txt files in /tmp directory.
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

  1. Step 1: Understand the command

    The command finds all files ending with .txt in /tmp and runs echo File: {} on each.
  2. Step 2: Predict output

    For each file found, it prints "File: " followed by the full file path (because {} is replaced by the path).
  3. Final Answer:

    Prints 'File: ' followed by each .txt file path found in /tmp. -> Option D
  4. 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

  1. Step 1: Check -exec syntax

    The -exec command must end with \;, not just ;.
  2. Step 2: Validate other parts

    Quotes around '*.log' are correct, and gzip is appropriate to compress logs.
  3. Final Answer:

    Missing backslash before semicolon in -exec ending. -> Option A
  4. 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

  1. Step 1: Understand size filter and command

    -size +5M finds files larger than 5MB. chmod 644 {} changes permissions of each found file.
  2. 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.
  3. Final Answer:

    find /home/user -size +5M -exec chmod 644 {} \; -> Option A
  4. Quick Check:

    Use -exec with {} and end with \; [OK]
Hint: Use {} for file and end -exec with \; [OK]
Common Mistakes:
  • Omitting {} placeholder
  • Missing backslash before semicolon
  • Leaving out semicolon entirely
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard