JPG batch compression & rename (find -exec, xargs, piping?)

convert writes to a different image file. To overwrite original image files use mogrify.

Single file:

mogrify -resize 50% Pic.jpg 

All .jpg files:

mogrify -resize 50% *.jpg

You could use this one liner: for img in $(ls *.jpg); do convert $img -resize 50% $img;done;


Try this:

SIZE=50 ; find -iname "*.jpg" | while read line ; do NF="$(echo $line | sed -r "s/(\.jpg|\.JPG)/_$SIZE\1/")" ; convert "$line" -resize $SIZE% "$NF" ; done

That's really not just one line. Here's an indented version:

SIZE=50
find -iname "*.jpg" | while read line ; do
  NF="$(echo $line | sed -r "s/(\.jpg|\.JPG)/_$SIZE\1/")"
  convert "$line" -resize $SIZE% "$NF"
done

You can adjust the value of SIZE to any other size, in percentage.

Please notice that after the first run, it will also take the already converted files as input files. You can skip files named like _NUMBER.jpg by modifying the find parameter, or you can also store the converted files somewhere else.


This script will do it for you!

Save it in a file and make the file executable with: chmod +x filename.

#!/bin/bash
SAVEIFS=$IFS
IFS=$(echo -en "\n\b")
C=0;
DES="./";       # Destination directory

for img in `find . -maxdepth 1 -mindepth 1 -iname '*.jpg' -type f`
do
    (( ++C ));
    NF="${DES}Pic_lorez_${C}.jpg";      # New file name
    convert $img -resize 50% $NF;
done

# restore $IFS
IFS=$SAVEIFS
  • Change the ./ to the directory where you want to put converted images, by default it will put them in current directory.
  • Change Pic_lorez_ to whatever name you want to give to converted files.