I use command of find, for example:
find . -name "*.log" -exec grep "running" {} \;
why the command of find need {}
, a blank and \
?
This is because of the -exec
parameter: the {}
is a placeholder for the file that will be passed to the command.
the semicolon (;
) tells find
that the -exec
argument list is over, but since ;
is also a shell operator, you need to escape it so that it reaches find
: \;
-exec
works like this: for every file that is found, the first argument after -exec
(the command) is executed and all parameters up until the ;
are passed as arguments to the command. The {}
is then replaced by the current filename that is found by find
.
{}
is a placeholder for path, which find
replaces with the actual found path.
\;
terminates find's exec
arguments. Without \
shell would treat it as shell statement terminator, hence it needs to be quoted with \
for the shell to pass ;
it to find.
I'd say that ;
was an unfortunate choice for find
exec
command terminator.
Note that {} \;
sequence can be replaced with {} +
:
-exec command {} +
This variant of the -exec action runs the specified command on
the selected files, but the command line is built by appending
each selected file name at the end; the total number of invoca‐
tions of the command will be much less than the number of
matched files. The command line is built in much the same way
that xargs builds its command lines. Only one instance of `{}'
is allowed within the command. The command is executed in the
starting directory.
Just read the manpages
-exec command ;
Execute command; true if 0 status is returned. All following
arguments to find are taken to be arguments to the command until
an argument consisting of `;' is encountered. The string `{}'
is replaced by the current file name being processed everywhere
it occurs in the arguments to the command, not just in arguments
where it is alone, as in some versions of find. Both of these
constructions might need to be escaped (with a `\') or quoted to
protect them from expansion by the shell. See the EXAMPLES sec‐
tion for examples of the use of the -exec option. The specified
command is run once for each matched file. The command is exe‐
cuted in the starting directory. There are unavoidable secu‐
rity problems surrounding use of the -exec action; you should
use the -execdir option instead.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With