What is meaning of {} + in find's -exec command?

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:

  • man find
  • Using semicolon (;) vs plus (+) with exec in find at SO
  • Simple unix command, what is the {} and \; for at SO

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

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.

Tags:

Find