Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. It's 100% free, no registration required.

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 want to write a bash script to convert every .pdf file in the current directory into a .png file. For example:

$ls .
a.pdf b.pdf
Converting pdfs to pngs
a.pdf -> a.png
b.pdf -> b.png

This is my best attempt:

convert -verbose -density 500 -resize '800' a.pdf a.png
convert -verbose -density 500 -resize '800' b.pdf b.png
share|improve this question
up vote 7 down vote accepted

If you have really strange names, ones that contain newlines or backslashes and the like, you could do something like this:

find . -type f -name '*.pdf' -print0 |
  while IFS= read -r -d '' file
    do convert -verbose -density 500 -resize 800 "${file}" "${file%.*}.png"

That should be able to deal with just about anything you throw at it.

Tricks used:

  • find ... -print0 : causes find to print its results separated by the null character, let's us deal with newlines.
  • IFS= : this will disable word splitting, needed to deal with white space.
  • read -r: disables interpreting of backslash escape characters, to deal with files that contain backslashes.
  • read -d '': sets the record delimiter to the null character, to deal with find's output and correctly handle file names with newline characters.
  • ${file%.*}.png : uses the shell's inbuilt string manipulation abilities to remove the extension.
share|improve this answer
Both answers are good but I accepted this one because it comes with an explanation of the tricks used. – I Like to Code Mar 25 '14 at 14:13

You can use bash for loop as follows:

for pdfile in *.pdf ; do
  convert -verbose -density 500 -resize '800' "${pdfile}" "${pdfile%.*}".png
share|improve this answer

You could use mogrify to batch convert & resize all .pdfs in the current directory:

mogrify -verbose -density 500 -resize 800 -format png ./*.pdf

when using a different format (in this case -format png) the original .pdfs are left untouched, the output files having the same basename + the extension specified by format.

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.