The image resolution of today’s photographic devices such as smartphones and digital cameras is increasing day by day. The real trick comes when we have to share these images, upload them to a cloud or even store them on a device with limited storage space. Graphic designers have to deal with the same problem, but they are well equipped with tools that can compress the images so that they can be easily shared.

For Debian there are many graphical tools that would allow you to optimize and compress your jpegs, but here we will discuss a command line tool called Jpegoptim. This is especially useful for terminal-experienced users who want to perform their tasks with minimal system resources. Jpegoptim can help you to compress your jpegs, jpg and jfif files with and without quality loss, depending on what you are looking for.

In this article we describe how to install the command line utility Jpegoptim on your Debian. We will also explain different ways you can use this program to optimize and compress your jpeg image files. The article will also enable you to write and run a bash script that automates the process of compressing all jpeg files that are located in a single directory.

We have run the commands and procedures mentioned in this article on a Debian 10 Buster system.

Installing Jpegoptim on Debian

Jpegoptim is easily available through the official Debian repository and can be easily installed through the command line using the apt-get command.

Open your Terminal application through system Application Launcher Search as follows:

Debian Terminal

The next step is to update your system’s repository index through the following command:

$ sudo apt-get update

Update packages

This helps you in installing the latest available version of software from the Internet. Please note that only an authorized user can add, remove and configure software on Debian.

Now you are ready to install Image Jpegoptim; you can do so by running the following command as sudo:

$ sudo apt-get install jpegoptim

Install Jpegoptim

The system might ask you the password for sudo and also provide you with a Y/n option to continue the installation. Enter Y and then hit enter; the software will be installed on your system. The process may, however, take some time depending on your Internet speed.

You can check the version number of the application, and also verify that it is indeed installed on your system, through the following command:

$ jpegoptim --version

Check jpegoptim version

Using Jpegoptim for Image Compression

Let us now explore the power of Jpegoptim through the following ways you can use it:

Losslessly compress a single image

If you want to compress a single image without wanting to compromise on the quality, use the following command:

$ jpegoptim image_name.jpg


$ jpegoptim sample.jpg

Optimize jpeg image

Note: Please keep in mind that the tool overwrites the existing image, so it is a good idea to have the original image saved somewhere else.

If you want the image to be compressed even further, you can run the same command again. However, Jpegoptim will start skipping the compression if it reaches the limit where you have a lossless image quality with the most compression.

Here is how the output would look like in such a situation:

Reduced size of jpeg image

Compress an image to another folder

If you are afraid that you will lose your original image because Jpegoptim will overwrite it, you can tell the tool to save the compressed image to another folder.

First, create a folder where you want the compressed images to be saved. If you want them to be saved to an already existing folder then you do not need to create a new one. You can then use the following command syntax to perform the compression:

$ jpegoptim -d ./[destination-folder] -p [image_name].jpg

For example:

$ jpegoptim -d ./compressed -p sample.jpg

Store optimized image in a different folder

The above command will compress and save my sample.jpg file to an already existing folder named “compressed”.

Check Potential Compression

If you want to see, beforehand, how much an image is capable of being compressed, you can simulate image compression through Jpegoptim. The following command will not actually compress the image, it will just tell you how much the image will be compressed if you ever try to compress it with Jpegoptim.

The following example will further explain the process:

I have a jpg image named sample.jpg. I will print its size first through the du command as follows:

$ du sample .jpg

Then, I will use the -n flag with the jpegoptim command to know about its potential compression.

$ jpegoptim -n sample.jpg

Check potential space savings of jpeg image

When I rechecked its size after using the jpegoptim command as above, I saw no change in its size. This ensures that the jpeg command with the -n flag only displays the future compression percentage and size without actually compressing the image.

Compress Images “with” a loss in quality

Sometimes we want to compress our images knowing that the process will not be lossless. The good thing is that with Jpegoptim you can specify how much quality loss you are ok with. There are two ways you can “lossily” compress your images;

1. Using the -m flag to specify quality factor/percentage: Sets the maximum image quality factor (disables lossless optimization mode, which is by default enabled). This option will reduce the quality of those source files that were saved using a higher quality setting. While files that already have a lower quality setting will be compressed using the lossless optimization method.

Here is how you can specify the compression percentage:

$ jpegoptim -m[percentage_in_numbers] image.jpg

Lossy compression

This was my original image:

Test image

And this is how it looks like with a 10 percent quality factor:

Compressed image

2. Using the –size option to specify the size of the compressed image: Try to optimize file to a given size (disables lossless optimization mode). Target size is specified either in kilobytes (1 – n)

or as a percentage (1% – 99%) of the original file size.

This is how you can specify size in kbs for the resulting image:

$ jpegoptim –size=[size-in-kb] image_name.jpg

Batch Optimizing Image files

There are several ways in which you can use Jpegoptim to compress multiple files at once.

The first one is to specify all the files you want to compress in a single jpegoptim command as follows:

$ jpegoptim file1.jpeg file2.jpg file3.jpg

The command will overwrite all the specified files and with the newly compressed ones.

The second method is to tell Jpegoptim to compress all the files of a certain type, for example jpg, all residing in the same folder:

$ jpegoptim *.jpg

In order to see how this command works, I listed all the files in my Pictures folder with the -l flag. This command would print the total size of all the images residing in the current folder:

$ ls -l

List of images

Then I used the same ls -l command to see how much difference has the Jpegoptim command made to the total size of all files. You can see the difference in total bytes in the following output:

Optimize all files in a given folder

These were just two files; you can save a lot of space like this. The beauty of the whole process is that you are not compromising on the quality of your valuable images. You can, of course, do the same with other file formats supported by Jpedoptim.

The third and a very useful option is that you can even make use of a shell script to automate compressing all files of a single type located in the current directory. Here is how to do so:

Bash Script to compress all jpg files in the current directory

This section will explain how you can write a shell script that will compress all jpgs located in the current directory to a folder named “compressed”. The script will not create the folder; it will just save the output to an already created folder by this name.

First, move to the Documents folder where we will create the script:

$ cd Documents

Note: You can save the script anywhere you like. It is advisable that you create all the scripts in the same folder so that you do not lose track of where you have saved them. I personally prefer saving all my scripts in the Documents folder.

Open a new script file in one of your favorite text editors. We will use the nano editor in order to open an empty script file by the name of

$ nano

In that empty file, add the following script.


# compress all *.jpg files in the current directory

# and place them in ./compressed directory

# with the same modification date as original files.

for i in *.jpg; do jpegoptim -d ./compressed -p “$i”; done


Tip: Instead of typing the whole script into you bash file, you can copy it from here and paste in the Terminal by using the Ctrl+Shift+V, or by using the Paste option from the right-click menu.

This is how your file would look like:

Script to compress images with jpegoptim

Now, exit the file through the Ctrl+X shortcut. You will then be asked to save the file on the “Save modified buffer?” prompt. Type Y and then hit Enter; your script file is now saved in the current folder.

In order to make this file executable by the current user, run the following command in your Terminal:

$ chmod +x

In order to run the script, change your current directory to the one whose jpg’s you want to compress (in my case the Pictures directory). Make sure that the directory contains a folder named “compressed”. If it is not there, please create one before running the script.

Finally, run the script as follows:

$ /home/[username]/Documents/

This command will execute the bash script from the folder you created it in. This is the reason you had to specify the entire path to that .sh file.

compression script in action

You will see that all the compressed files will be written in the “compressed” folder. You can verify this through the following command:

$ ls compressed

This was all about image optimization through the Jpegoptim utility. For further details you can see the manpage through the following command:

$ man jpegoptim

Now sharing your images over the Internet and uploading them to a bandwidth and storage restricted location should not be a problem

Optimize Jpeg/jpg Images through the Debian Command line

Karim Buzdar

About the Author: Karim Buzdar holds a degree in telecommunication engineering and holds several sysadmin certifications. As an IT engineer and technical author, he writes for various web sites. You can reach Karim on LinkedIn