Find Command – Understanding find(1)’s -exec Option with Curly Braces and Plus Sign

find

Using the following command, could someone please explain what exactly is the purpose for the ending curly braces ({}) and plus sign (+)?

And how would the command operate differently if they were excluded from the command?

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

Best Answer

The curly braces will be replaced by the results of the find command, and the chmod will be run on each of them. The + makes find attempt to run as few commands as possible (so, chmod 775 file1 file2 file3 as opposed to chmod 755 file1,chmod 755 file2,chmod 755 file3). Without them the command just gives an error. This is all explained in man find:

-exec command ;

      Execute command; true if 0 status is returned.  All following arguments to find are taken to be arguments to the command until an argument consisting of ‘;’ is encountered.  The string ‘{}’ is replaced by the current file name being processed everywhere it occurs in the arguments to the command, not just in arguments where it is alone, as in some versions of 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. …

Related Question