9

I use command of find, for example:

find . -name "*.log" -exec grep "running" {} \;

why the command of find need {} , a blank and \?

4 Answers 4

15

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.

2

{} 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.
1

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.
0

The backslash is an escape to protect the semicolon from being misinterpreted. The braces are placeholders for the full path outputted by find.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.