Take the 2-minute tour ×
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.

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
add comment

3 Answers

up vote 7 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
3  
$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
1  
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
add comment

You can use mogrify:

mogrify -format png *.bmp

Source: Imagemagick website

share|improve this answer
add comment

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
  do
    DIR=$(dirname "$BMP")
    PNG="$(echo $BMP | sed 's/.bmp//g')"
    convert "${BMP}" "${PNG}".png
  done
share|improve this answer
1  
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
1  
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
add comment

Your Answer

 
discard

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.