What's the +
in find /path/ -exec command '{}' +
do? as opposed to find /path/ -exec command '{}' \;

- 59,188
- 74
- 187
- 252
2 Answers
The '+' makes one big command line out of all found files to minimize the number of commands to be run.
Given the case that a find command finds four files.
find . -type f -exec command '{}' \;
would produce
command file1
command file2
command file3
command file4
On the other hand
find . -type f -exec command '{}' \+
produces
command file1 file2 file3 file4

- 4,597
From the man page:
-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.

- 59,188
- 74
- 187
- 252

- 6,350
- 1
- 28
- 23
-
1man that's very... terse? I find it to be lacking in depth, and clarity. – xenoterracide Nov 01 '10 at 12:54
-
The idea is not to blindly spawn one process for each file, but to gather the filenames together and call the command fewer times. An example: instead of creating 3 different "rm FILENAME" processes, it gathers the filenames and does "rm FILE1 FILE2 FILE3". – tante Nov 01 '10 at 13:02
-
IIRC some versions of xargs have an option to batch these names together up to a specified limit, so you can benefit from this but also avoid overflowing when you have a large list, too. – Kevin Cantu Nov 02 '10 at 02:03
-
-
xargs
,-exec … {} +
does the same as-print | xargs …
, but without the quoting issues (-print0 | xargs -0
is another way, but it's less portable). – Gilles 'SO- stop being evil' Nov 01 '10 at 18:13