3

I would like to do something like

find ./ -type f | parallel --gnu convert "{}" "$(basename "{}" pdf)jpg"

But it does not work (the files are renamed to filename.pdfjpg). I think the problem is that the subprocess is executed right away (even before calling parallel). I would like the subprocess to be executed for each file.

Thanks to find -exec with multiple commands I can do:

find *.pdf -exec sh -c 'convert "$1" "$(basename "$1" pdf)png"' _ {} \;

but I would like to use GNU parallel. The following does not work:

find ./ -type f | parallel --gnu sh -c 'convert "$1" "$(basename "$1" pdf)jpg"' _ {}

Of course, I can do this with two commands (e.g. using rename) but I would like to learn how to do it with one and with GNU parallel.

Community
  • 1
  • 1
Xu Wang
  • 10,199
  • 6
  • 44
  • 78

1 Answers1

4

If all the files have the .pdf extension, you can use GNU parallel's extension stripping syntax:

find ./ -type f | parallel --gnu convert {} {.}.jpg

(No need to quote {}; parallel does it for you.)

Otherwise, you have to use bash -c:

find ./ -type f | parallel --gnu bash -c 'convert "$1" "${1/%.pdf}.jpg"' bash {}

(The second bash is there because $1 is actually the second argument after the command string. Many people like to use _, since the value doesn't really matter.)

rici
  • 234,347
  • 28
  • 237
  • 341