1

To start with, I've read related subjects, but none of them has explained my doubts.

I have a following command which works fine:

find ./source/ -maxdepth 1 -type f ! -name "secretinfo.md" -exec cp {} ./build/ \;

However, using ; means that I'm passing every found file to the cp command one by one. I thought I could pass them all at once as cp command allows that. Based on other answear that I've found:

A -exec command must be terminated with a ; (so you usually need to type \; or ';' to avoid interpretion by the shell) or a +. The difference is that with ;, the command is called once per file, with +, it is called just as few times as possible (usually once, but there is a maximum length for a command line, so it might be split up) with all filenames

Since that's what I did:

find ./source/ -maxdepth 1 -type f ! -name "secretinfo.md" -exec cp {} ./build/ +

And here I'm getting an error:

find: missing argument to `-exec'

Can you explain to me why this is not allowed?

Paweł Poręba
  • 1,084
  • 1
  • 14
  • 37

1 Answers1

1

+ is ok, but your use of parameter with exec is not correct.

you may try:

find ./source/ -maxdepth 1 -type f ! -name "secretinfo.md" -exec bash -c 'cp $@ ./build/' - {} +

or

find ./source/ -maxdepth 1 -type f ! -name "secretinfo.md" -exec cp --target-directory=./build/ {} +

Despite it may not be rely satisfying, the reason for why is that like this may be because it is supposed to; in the man find you may see:

-exec command {} +

and you may notice the command is before the {} + (which is somehow harcdoded in this syntax).

I must say, I don't know why it specified like this (seems to be posix: http://pubs.opengroup.org/onlinepubs/009695399/utilities/find.html)

see also

Solution to error 'find: missing argument to -exec' with find -exec cp {} TARGET_DIR +

for some other examples, see find: missing argument to -exec

OznOg
  • 4,440
  • 2
  • 26
  • 35