Search code examples
bashimagemagickbatch-rename

Batch resize and rename image files in subdirectories


I'm trying to resize and rename several hundred subdirectories of images. The files that I need to be changed:

  • End with A.jpg
  • Need to be resized down to 400x400
  • Renamed to A@2x.jpg

Example: images/**/A6919994719A@2x.jpg

I got the resizing bit down in one directory. I'm having some trouble finding a way to rename just the end of the file, not the extension, and executing it through the subdirectories.

Any help would be appreciated.

#!/bin/bash

for i in $( ls *A.jpg); do convert -resize 400x400 $i

Solution

  • You can do this:

    #!/bin/bash
    find . -name "*A.jpg" | while read f
    do
       newname=${f/A.jpg/A@2.jpg}
       echo convert "$f" -resize 400x400 "$newname"
    done
    

    Remove the word echo if it looks correct, and run only on files you have backed up.

    You can also do it in a one-liner, if you really want to:

    find . -name "*A.jpg" -exec bash -c 'old="$0";new=${old/A.jpg/A@2.jpg};echo convert "$old" -resize 400x400 "$new"' {} \;