Resize images and save with new name recursively with bash

Posted on 6. May 2013

When we prepared the relaunch of diginights with our new 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-}"'
Made with ♥️ and Gatsby © 2024