19

I want to know the meaning of {} + in the exec command, and what is the difference between {} + and {} \;. To be exact, what is the difference between these two:

find . -type f -exec chmod 775 {} +
find . -type f -exec chmod 775 {} \;

migrated from serverfault.com Apr 13 '15 at 11:21

This question came from our site for system and network administrators.

  • 5
    It is described in man find(1) in much detail, easily to be find by anyone. Don't be lazy. – Miroslav Koškár Apr 10 '15 at 8:36
  • Thanks dear Miroslav, i searched in man exec ! – Mohsen Apr 10 '15 at 9:28
  • Ah, I had never come across the {} + usage before. I normally use xargs for that (and often find ... -print0 | xargs -0 ...) – Cameron Kerr Apr 10 '15 at 10:19
22

Using ; (semicolon) or + (plus sign) is mandatory in order to terminate the shell commands invoked by -exec/execdir.

The difference between ; (semicolon) or + (plus sign) is how the arguments are passed into find's -exec/-execdir parameter. For example:

  • using ; will execute multiple commands (separately for each argument),

    Example:

    $ find /etc/rc* -exec echo Arg: {} ';'
    Arg: /etc/rc.common
    Arg: /etc/rc.common~previous
    Arg: /etc/rc.local
    Arg: /etc/rc.netboot
    

    All following arguments to find are taken to be arguments to the command.

    The string {} is replaced by the current file name being processed.

  • using + will execute the least possible commands (as the arguments are combined together). It's very similar to how xargs command works, so it will use as many arguments per command as possible to avoid exceeding the maximum limit of arguments per line.

    Example:

    $ find /etc/rc* -exec echo Arg: {} '+'
    Arg: /etc/rc.common /etc/rc.common~previous /etc/rc.local /etc/rc.netboot
    

    The command line is built by appending each selected file name at the end.

    Only one instance of {} is allowed within the command.

See also:

  • There is a difference and a major one. See user1116056's answer – Jahid Jul 18 '16 at 5:44
21

Given that the command find gets below three files:

fileA
fileB
fileC

If you use -exec with a plus(+) sign,

find . -type f -exec chmod 775 {} +  

it will be:

chmod 775 fileA fileB fileC

The command line is built by appending each matched file name at the end, which is in the same way that xargs builds its command lines. The total number of invocations of the command (chmod, in this case) will be much less than the number of matched files.

If you use -exec with a semicolon(;),

find . -type f -exec chmod 775 {} \;

it will be:

chmod 775 fileA
chmod 775 fileB
chmod 775 fileC
5

As per man find:

-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 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. Only one instance of ‘{}’ is allowed within the command. The command is executed in the starting directory.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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