How to batch compress images with Imagemagick or similar command or program in Linux

Solution 1:

The naming is a slightly different format, but:

for img in *.jpg; do
  convert -resize 20% "$img" "opt-$img"
done

Solution 2:

Using a 'for' loop will definitely work - and is a good general technique - but you almost certainly have more than 1 processor on your machine, so why do just one conversion at a time?

You can get things moving a lot quicker if you do:

find . -name '*.jpg' | xargs -n1 -P8 -I{} convert -resize 20% "{}" "opt-{}"

The arguments to xargs are:

n1  - Only give 'convert' one file at a time
P8  - Use 8 processes
I{} - Replace {} with the filename given by 'find'

And then the convert command is given afterwards.