5

I am trying to convert a bunch of images to thumbnails that might be in several subdirectories, and save a copy of the image as a thumbnail inside the subdirectory, but I can't figure out the syntax. Any advice would be welcome.

convert test/*/*.jpg -50x50 test/*/*_thumb.jpg

2 Answers 2

6

You should use find for finding all files you need.

find test -iname *.jpg -exec convert {} -resize 50x50 {}_thumb \;

And then you should rename files with name *.jpg_thumb to *_thumb.jpg by using:

find -name *.jpg_thumb -exec rename -e 's/^(.*)(.jpg)(_thumb)$/\1\3\2/' '{}' \;

Note that -50x50 is not a legal parameter for convert. You should use -resize parameter with value 50x50 like in example above.

6

A shell loop should also do:

for file in test/*/*.jpg ; do
    new=${file%.jpg}_thumb.jpg 
    convert "$file" -resize 50x50 "$new"
done

Or with Bash's globstar, which is better if the files can be on arbitrary directory levels:

shopt -s globstar
for file in test/**/*.jpg ; do ...

${file%.jpg} takes the value of the variable file and removes the string .jpg from the end. (see BashGuide on parameter expansions)

0

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .