Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

According to the man find -exec option must end with ;.

The expression must be terminated by a semicolon (``;'').

But when I use find-grep in Emacs, the expression end with +. Which one should I use? Is there difference two expression?

share|improve this question
add comment

2 Answers

up vote 6 down vote accepted

This is from the manual for GNU find:

   -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 `;'

Cut off by me. But we also have:

   -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 num‐
          ber of invocations 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.

Cut off by me.

So, ; and + do different things. A short example in code to demonstrate:

$ mkdir test
$ touch test/{a,b}
$ find test -exec echo foo {} \;
foo test
foo test/a
foo test/b
$ find test -exec echo foo {} +
foo test test/a test/b

+ only generates a single invocation with all matching files given as an argument list (in practice it behaves like xargs). ; executes the command once for each matching file (the ; needs to be escaped in the shell; hence the preceding \).

Note that some versions of find lack the + option, but the GNU version is most likely installed on anything non-embedded Linux.

share|improve this answer
    
Note also the -execdir option. It acts like -exec, but it creates a separate command for separate folder. It's used when the calling command may be confused by starting in the wrong folder. You can use ; or + (with the same meaning as for -exec-) with -execdir. –  Paddy Landau Sep 11 '13 at 18:27
add comment

TL;DR:

Using the + in -exec is the same as -exec, except that {} is replaced with as many pathnames as possible for each invocation of utility.


Slightly longer from man find on my computer:

   -exec utility [argument ...] ;
             True if the program named utility returns a zero value
             as its exit status.  Optional arguments may be passed
             to the utility.
             The expression must be terminated by a semicolon.  If you
             invoke find from a shell you may need to quote the 
             semicolon if the shell would otherwise treat it as a control
             operator.  If the string ``{}'' appears anywhere in the 
             utility name or the arguments it is replaced by the pathname
             of the current file.
             Utility will be executed from the directory from which find was
             executed.  Utility and arguments are not subject to the further
             expansion of shell patterns and constructs.


     -exec utility [argument ...] {} +
             Same as -exec, except that `{}` is replaced with as many 
             pathnames as possible for each invocation of utility.  
             This behaviour is similar to that of xargs(1).

Notice the second paragraph. Do not stop reading after the first paragraph explaining -exec.

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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