1

I need to get all of the jpg files in a directory, and execute the following command on each one:

mycommand -in <imagebasename>.jpg -out <imagebasename>.tif --otherparam paramvalue

I thought about:

find . -name "*.jpg" -exec mycommand -in {} -out {}.tif --otherparam paramvalue\;

but this will pass something like "./<imagebasename>.jpg" to mycommand. I need to pass <imagebasename> only instead.

I don't need to process the directories recursively.

Alex
  • 129
  • 2
  • 12

1 Answers1

3

Try:

find . -name "*.jpg" -exec sh -c 'mycommand -in $0 -out "${0%.*}.tif" --otherparam paramvalue' {} \;

This will pass command of the form mycommand -in <imagebasename>.jpg -out <imagebasename>.tif --otherparam paramvalue to -exec.

EDIT: For removing leading ./, you could say:

find . -name "*.jpg" -exec bash -c 'f={}; f=${f/.\//}; echo mycommand -in "${f}" -out "${f%.*}.tif" --otherparam paramvalue' {} \;

(Note that the interpreter for -exec has changed.)

devnull
  • 118,548
  • 33
  • 236
  • 227
  • this will produce `mycommand -in ./imagebasename.jpg -out ./imagebasename.tif --otherparam paramvalue`. How to strip the leading "./" ? – Alex Aug 07 '13 at 08:43
  • As my answer was based on yours in one of my questions... let's just leave your answer! This is endogamy :D – fedorqui Aug 07 '13 at 09:02
  • @fedorqui Nope, I wouldn't think or say so. – devnull Aug 07 '13 at 09:03
  • @fedorqui On the contrary, it's a matter of figuring where to apply a given solution. *Even that* takes something. – devnull Aug 07 '13 at 09:05
  • @devnull I mean endogamy in the funny sense: us using and reusing questions and answers. As both your solution and mine became very similar, I now prefer to keep yours as what I wrote was based in what you taught me. – fedorqui Aug 07 '13 at 09:07