0

I'm trying to write a script which finds certain files and executes a command with the files it finds. Doesn't sound too hard, but I'm having a lot of trouble getting it to work. Basically this is what I'm doing:

find /Users/Me/MyFolder -name '*filename.tsv' -print0 -exec /usr/bin/java -jar -Xmx2G /Volumes/Macintosh\ HD/Applications/mMyApp/MyApp.jar -s "," -e "." {}  > {}-new.tsv \;

The result is a file called {}-new.tsv which contains the output of MyApp.jar for all files that are found.

Can anyone shed some light on what I'm doing wrong?

  • How about if you enclose the -exec statement like this: -exec '/usr/bin/java -jar -Xmx2G /Volumes/Macintosh\ HD/Applications/mMyApp/MyApp.jar -s "," -e "." {} > {}-new.tsv' \; – user34720 Jun 9 '16 at 10:41
  • @nwilder I tried that, but for some reason this results in a "No such file or directory" error.. – Lorenzo Jun 9 '16 at 10:45
  • 1
0

Okay, I solved it. Apparently the braces have to be masked:

find /Users/Me/MyFolder -name '*filename.tsv' -print0 -exec bash -c '/usr/bin/java -jar -Xmx2G /Volumes/Macintosh\ HD/Applications/mMyApp/MyApp.jar -s "," -e "." "$1"  > "$1-new.tsv"' {} \;

Furthermore, the command has to be invoked by using bash -c 'command string' (make sure to escape single quotes)

  • I think you'd want to drop the backslashes in front of the "s as otherwise they'd been passed literally to java which I don't think is intended. – Stéphane Chazelas Jun 9 '16 at 11:23
  • Note that you don't need to use bash. sh will do just as well. – Stéphane Chazelas Jun 9 '16 at 11:24
  • Also note that that $0 is also used for error messages, a more idiomatic way to write it is -exec sh -c '... "$1"' sh {} \; or to avoid running so many sh commands: -exec sh -c 'for file do; ... "$file"; done' sh {} +. – Stéphane Chazelas Jun 9 '16 at 11:27

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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