Resize images and save with new name recursively with bash

When we prepared the relaunch of diginights.com with our new and awesome responsive layout, we needed to resize a bunch of flyer, location and avatar images (~1.000.000). One of our requirements was that we save the images with a new tag so we won’t affect the running application. The following bash one-liner will find all images recursively in a directory and resizes them to a specific size and replaces a given string in the filename.

In the example, we search for files which match the pattern ‘preview_image*-default-*’ and resize them to a width of 238 pixel. The new image will be saved in the same path, but in the filename, we replace the string ‘-default-‘ with ‘-resp-‘.

find . -name 'preview_image*-default-*' -print0 | xargs -0 -L 1 -I {} bash -c 'f="{}"; convert -compress JPEG -resize 238 "$f" "${f/-default-/-resp-}"'


Passion for sports with boards, climbing, mountain biking, traveling, photography, videography and software engineering. /n/n Heilbronn // Germany

One Comment

Leave a Reply