Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

why find need "{} \"?

Tags:

linux

bash

I use command of find, for example:

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

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

like image 360
lxgeek Avatar asked Sep 14 '12 08:09

lxgeek


3 Answers

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.

like image 140
fat-lobyte Avatar answered Nov 15 '22 18:11

fat-lobyte


{} 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.
like image 43
Maxim Egorushkin Avatar answered Nov 15 '22 20:11

Maxim Egorushkin


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.
like image 30
stefan bachert Avatar answered Nov 15 '22 18:11

stefan bachert