0

Short question is: how to call some command or a script once with all found names as arguments plus some extra arguments?

Long question is:

Why does this work (prints all file names 1 name per line):

find . -type f -exec sh -c "echo {}" \;

but this doesn't (prints nothing):

find . -type f -exec sh -c "echo {}" +

However, this works (prints all file names in a line):

find . -type f -exec echo {} +

but this fails with message find: missing argument to '-exec' :

find . -type f -exec echo {} more args +

However, this prints file names but not more args:

find . -type f -exec echo '{} more args' +

(prints ./file1.txt ./file2.txt while I expected ./file1.txt ./file2.txt more args)

I know there is a limit on how long the argument list can be, but I tested it with just a few file names so it shouldn't matter.

  • 1
    Try either find . -type f -exec sh -c 'echo "$1"' sh {} \; or find . -type f -exec sh -c 'echo "$@"' sh {} +. – Satō Katsura May 15 '17 at 6:32
  • @SatoKatsura Seems to work, though I don't understand it. What does the second sh do? – spiderface May 15 '17 at 6:42
  • Sets $0 for sh. – Satō Katsura May 15 '17 at 6:44
  • So the answer is that it HAS to end with a {} + and I can't put the {} anywhere else, even inside quotes? – spiderface May 15 '17 at 6:59
  • Nope, you shouldn't put {} inside quotes. – Satō Katsura May 15 '17 at 7:02

Browse other questions tagged or ask your own question.