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

bashcommand linecompressionimageslinux

I recently learned about the convert command from imagemagick which I have used to compress many pictures that I have for personal use, and for a blog that I have. In my experience, Imagemagick is the best image compressor program, and it gives one the most control over how to compress images. I have many more pictures that I want to compress, but don't want to have to use the convert command so many times, one by one for every file. The main problem is that it is very time consuming for me to do it. I would like to be able to one big batch way to compress my images. I am OK with doing this via command line, but a GUI might make this a little bit more intuitive.

Here is a sample of a command that I use to make the original image 20% of the original size.
convert -resize 20% 20140322_102113.jpg 20140322_102113opt.jpg

If I say have 100 images, and they are all in the same folder, I want to be able to do something like the following

For all images
convert -resize 20% imagename.jpg imagename_optimized.jpg

I don't know if there is a command that can already do this, but if not, I thought about creating a bash command, but I am not so familiar with bash. Help creating this simple bash script, or advice on how to solve my dilemma is appreciated. I use Linux, and would only like a solution specifically for Linux.

Best Answer

The naming is a slightly different format, but:

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