4

I have some times come across the use of trailing + while using unix commands like find.

Example from SO post -

find . -exec touch -t 201007162310.00 {} +

Please help me understand the use of this.

Kusalananda
  • 333,661
mtk
  • 27,530
  • 35
  • 94
  • 130

1 Answers1

10

From 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.

For example with find -exec touch -t 201007162310.00 {} +, if the find command without the -exec gives you the files 1.txt, 2.txt and 3.txt, it will execute:

touch -t 201007162310.00 1.txt
touch -t 201007162310.00 2.txt
touch -t 201007162310.00 3.txt

with -exec ... {} \;, and

touch -t 201007162310.00 1.txt 2.txt 3.txt

with -exec ... {} +.

The latter version is faster due to a (much) smaller number of new processes needed yet it is less portable (not all find implementations support it). And of course, if the command you try to -exec doesn't support receiving multiple files as arguments, it won't work.

ilkkachu
  • 138,973
lgeorget
  • 13,914
  • 3
    -exec {} + is POSIX and standard. For once, only the GNU find was not supporting it until findutils 4.2.12 (2005). -exec {} + was added to SysV some time in the 80s by David Korn. – Stéphane Chazelas Jun 07 '13 at 15:03