Cantech Knowledge Base

Your Go-To Hosting Resource

Compressing Images On The Linux Command Line

If you are looking to optimize and compress your images without compromising the original quality, before uploading them to local storage or cloud, command line tools can help you with this. Here are two commonly used tools:

  • jpegoptim: This specialized utility performs optimization and compression of JPEG files while maintaining original quality. The JPEG file achieves optimization through the adjustment of its Huffman coding tables.
  • OptiPNG: This stands as a specialized utility for PNG image optimization that achieves file size reduction while maintaining complete data integrity. It applies numerous compression techniques alongside delta filters and strives to minimize file size to its smallest potential.

This blog will guide you on how to install these two tools to compress pictures in Linux by using the command line.

Installation of jpegoptim and OptiPNG

For CentoOS or RPM based Linux Distribution

sudo yum install epel-release
sudo yum install jpegoptim optipng

For Debian and other APT-based Linux distributions

sudo apt-get install jpegoptim optipng

How to Use jpegoptim to compress JPEG files

1. Use the below command to compress only a single file .jpg

In this example, the original JPEG (.jpg) files are stored as ~/jpeg.

cd ~/jpeg
jpegoptim example.jpg

2. Use the below command to compress all the file .jpg files

cd ~/jpeg
jpegoptim *.jpg

Original files will be overwritten and replaced with the optimized and compressed version.

3. To keep the original files, specify a directory to save the compressed version by using the below command:

cd ~/jpeg
mkdir optim
jpegoptim *.jpg -d ~/jpeg/optim

4. To find more information about how to use the jpegoptim command, use the -h flag to get their help file:

jpegoptim -h

How to use OptiPNG to Compress and Optimize PNG, BMP, GIF, PNM, and TIFF files

Here are a some examples on how to use OptiPNG to add lossless compression to images

1. To compress a single file .png, use the below command:

cd ~/pic
optipng example.png

2. To optimize a .bmp file example.bmp:

cd ~/pic
optipng example.bmp

A file with a name example.png will be created in a source directory while the original example.bmp remains.

3. To compress all .png files in the source directory:

cd ~/pic
optipng *.png

Original files will be overwritten and replaced with the compressed version.

4. To keep the original files safe, use the -keep flag below:

cd ~/pic
optipng -keep *.png

Original files will stay as it is and suffixed with a .bak. For instance example.png.bak.

5. To keep the original files, specify the target directory to save the compressed version. Use the below command:

cd ~/pic
optipng -dir ~/pic/optim *.png

Original files will remain the same in the source directory and the compressed version will be saved in the target directory.

6. To find more information about how to use the OptiPNG command, use the -h flag to view its help file:

optipng -h
March 28, 2025