Find and Execute
Find and Execute
Using Find with Command Execution
There are two main ways to execute commands on files found by the find
command:
Method 1: Using -exec
with find
1
find . -exec cmd -option1 -option2 {} \;
Or for better performance with multiple files:
1
find . -exec cmd -option1 -option2 {} +
Method 2: Using xargs
1
find . -print0 | xargs -0 cmd -option1 -option2
Example
Find all backup files and list their details:
1
find . -name "*.bkp" -exec ls -l {} +
Notes
- The
{}
is replaced by the filename found byfind
- Using
+
at the end groups multiple files into a single command execution - The
-print0
and-0
options handle filenames with spaces correctly - For complex commands, the
xargs
method is often more flexible
Rule the world!
This post is licensed under
CC BY 4.0
by the author.