2
for f in $(ls mydir/*.jpg)
do 
    convert -thumbnail $size -quality $quality $f thumbsdir/$(basename $f)
done

I'm trying to code a script that takes a directory with images and generates thumbnails from these images. The script runs well, but when it gets to image files that include space characters, it gives me errors, because it thinks it's another file when it meets the space character.

I already tried using quotes such as:

convert -thumbnail $size -quality $quality "$f" "thumbsdir/$(basename $f)"

...but no success.

How could I make this work without having to rename all files and remove space characters?

1 Answer 1

4

Don't use ls.

for f in mydir/*.jpg
do 
    convert -thumbnail "$size" -quality "$quality" "$f" "thumbsdir/$(basename "$f")"
done

And you do need the quotes.

1
  • Cool. Double quotes inside double quotes, that's something I wasn't expecting. Thanks Oct 30, 2013 at 16:50

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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