Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I am able to use ImageMagick to create a thumbnail of the first page of a PDF using:

convert -thumbnail x80 95.pdf[0] thumb_95.png

This works fine and generates a thumb_95.png file.

I have tried several permutations of "find" using xargs but i cant get a combo working that will create the thumbnails in the folders along with the source PDFs.

The PDFs are in folders named with UUIDs, e.g.:


My required result would be:


Any help on the best ways to get this conversion to happen for all *.pdf recursively would be much appreciated!

share|improve this question

migrated from serverfault.com Feb 5 '13 at 14:02

This question came from our site for system and network administrators.

Also, a way to do the same with find: unix.stackexchange.com/questions/61656/… – njsg Feb 5 '13 at 14:44
up vote 0 down vote accepted

Try this:

find /source/directory -name "*.pdf" -exec \ sh -c 'convert -thumbnail x80 {} $(dirname {})/thumb_$(basename {})' \;

I had to modify it slightly to:

find /source/directory -name "*.pdf" -exec \ sh -c 'convert -thumbnail x80 {} $(dirname {})/thumb_$(basename {} .pdf)'.png \;

To have basename strip the filextension and then append .png.

Thanks to all answers. This one worked well for me!

share|improve this answer
I needed to modify it to: find /source/directory -name "*.pdf" -exec \ sh -c 'convert -thumbnail x80 {} $(dirname {})/thumb_$(basename {} .pdf)'.png \; to have basename strip the fileext. Thanks to all answers this one worked best for me! – fibble Feb 6 '13 at 15:53
Exercise to the reader: what happens if there's a file called $(rm -rf /).pdf somewhere in there. – Stéphane Chazelas Feb 6 '13 at 16:08
@StephaneChazelas: Good point. I had to originally add "sh -c..." to get the filename properly passed to the basename / dirname commands, but that opened up this security hole. If you are doing this on an untrusted directory, you can replace {} with '\''{}'\'', however you run into the same issue if a file is named '$(evil_command)'. Not sure of a correct way to close this one. – Derek Pressnall Feb 6 '13 at 17:48

The tricky part is to not have a filename of the form testfile.pdf.png as the output. Years ago I had the same problem and solved it with this helper script (adapted a bit):


NEWFILE=$(echo $FILE | sed "s:$SRCFRM\$:$TGTFRM:")
convert -thumbnail x80 $FILE[0] $NEWFILE

Store it somewhere, e.g. in your home dir, then use it like this:

find /path -name '*.pdf' -exec /path/to/converthelper.sh {} \;
share|improve this answer

You want to

  • Recurse into all PDFs:

    1. Enable globstar, with shopt -s globstar.
    2. Use it to generate a list of all the *.pdf files under the current directory: **/*.pdf
    3. Create a loop that iterates over said files:

      for filename in **/*.pdf
  • Now, you want to do something on the files: something is convert -thumbnail x80 95.pdf[0] thumb_95.png. There are several ways: I usually use basename, in this case you would need $(dirname $filename)/$(basename $filename .pdf).png, but another interesting (and simpler) approach is to use bash's string manipulation tools:

    1. Remove .pdf from the end of the filename: ${filename%.pdf}
    2. Add .png: ${filename%.pdf}.png

Finally, we can glue it all together (don't forget that this requires globstar, shopt -s globstar):

for filename in **/*.pdf
    convert -thumbnail x80 $filename[0] ${filename%.pdf}.png
share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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