Jpeg Image Optimization Linux - Compress Command

Categories:  Linux
Labels:  internet, bash script, optimization

How do I compress and optimize JPEG images on my amazon cloud account so that I can save bandwidth on cloudfront cdn account? How do I use an image compressor to create lossless compression on JPEG files, with no effect on image quality in bulk using Linux?

JPG file format is recommended for high resolution photographic-style images. You need to use jpegoptim command. It is used to optimize/compress jpeg files. Program supports lossless optimization, which is based on optimizing the Huffman tables. And so called “lossy” optimization where in addition to optimizing Huffman tables user can specify upperlimit for image quality.


Any commands with a # at the start means “as root”.

Type the following command:

# apt-get install jpegoptim


The syntax is:

jpegoptim file.jpeg
jpegoptim [options] file.jpeg

Type the following command to optimize photo.jpeg, enter:

$ jpegoptim photo.jpeg

Sample outputs:

photo.jpeg 1312x948 24bit JFIF  [OK] 25226 --> 10744 bytes (57.41%), optimized.

How do I process files in batch?

Use the bash for loop as follows:

for i in one.jpeg two.jpeg foo.jpeg; do jpegoptim "$i"; done


 ## process all *.jpeg in the current directory 
for i in *.jpeg; do jpegoptim "$i"; done


  1. jpegoptim home page.
  2. See man page for more information and examples - jpegoptim(1)
Related Posts: