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 have converted a single file from BMP to PNG with ImageMagick's convert using the command below

convert CD\ Front.bmp CD\ Front.png

I have many such files, thus I tried:

for f in */*.bmp ; do convert $f ${f%bmp}png; done

but this command hangs forever.

What am I doing wrong?

share|improve this question
up vote 10 down vote accepted

You most likely have a problem because you don't quote the file names:

for f in */*.bmp ; do convert "$f" "${f%bmp}png" ; done
#                             ^  ^ ^           ^

do this so the space in the file names do not cause problems.

share|improve this answer
$f should be quoted too – Mat Dec 15 '13 at 10:50
You are right, thank you for pointing out – Zelda Dec 15 '13 at 11:00
Quoting the variables is correct, but if you quote the glob then the shell will look for a literal */*.bmp. – evilsoup Dec 15 '13 at 12:04
Thanks to you as well. I should try out my solutions before posting... – Zelda Dec 15 '13 at 12:26

You can use mogrify:

mogrify -format png *.bmp

Source: Imagemagick website

share|improve this answer

I would refactor the code like this. I find this method to be clearer & easier to debug:

find . -type f -name '*.bmp' |\
  while read BMP
    DIR=$(dirname "$BMP")
    PNG="$(echo $BMP | sed 's/.bmp//g')"
    convert "${BMP}" "${PNG}".png
share|improve this answer
I took the liberty to change JPEG into PNG while reviewing – Anthon Dec 15 '13 at 17:09
Ahh, thanks. But it did work for JPEG’s as well! – JakeGould Dec 15 '13 at 17:10
Yes, guess it does, but it was not what the OP asked for ;-) – Anthon Dec 15 '13 at 17:21
Don't use find | while read, especially when a glob will suffice. – Kevin Dec 15 '13 at 18:17
And the sed would be better as 's/\.bmp$/.png/', then obviously remove the .png from the actual command – Kevin Dec 15 '13 at 18:19

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.