# Ubuntu – Run command recursively through subdirectories

bashscripts

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


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