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

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


Don't use ls.

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

And you do need the quotes.

  • 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.