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 {} ;
Answers:
Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.
Method 1
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.netbootAll following arguments to
findare 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 howxargscommand 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.netbootThe 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
Method 2
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
Method 3
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.
All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0