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.

5
  • No explanation in Find -exec + vs find | xargs. Which one to choose??
    – manatwork
    Jun 7, 2013 at 13:25
  • I don't see this as a duplicate of q#41740. This question asks "what", the other "why".
    – phunehehe
    Jun 7, 2013 at 14:48
  • Neither I consider it exact duplicate, however that question's first sentence definitely answers this one: “the -exec can take a + option to mimic the behaviour of xargs”.
    – manatwork
    Jun 7, 2013 at 14:53
  • 1
    Did you even look at the man page before asking this question?
    – mdpc
    Jun 7, 2013 at 16:13
  • @mdpc no. not yet got to completely read a man page yet ;)
    – mtk
    Jun 7, 2013 at 17:17

1 Answer 1

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 exec:

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

without the \+, and

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

with the \+.

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.

1
  • 2
    -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. Jun 7, 2013 at 15:03

Not the answer you're looking for? Browse other questions tagged or ask your own question.