0
0
Linux CLIscripting~5 mins

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

Choose your learning style9 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.