14

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

2 Answers 2

18

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
1
  • 3
    If you know xargs, -exec … {} + does the same as -print | xargs …, but without the quoting issues (-print0 | xargs -0 is another way, but it's less portable). Nov 1, 2010 at 18:13
4

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.

5
  • 1
    man that's very... terse? I find it to be lacking in depth, and clarity. Nov 1, 2010 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 1, 2010 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. Nov 2, 2010 at 2:03
  • (Find, itself, is a scary little language all its own!) Nov 2, 2010 at 2:03
  • find gives me a semi. Jan 29, 2011 at 21:02

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.