Script automatically optimize image with mozjpeg

by Daniel Pham
Published: Updated:

This article I will introduce to you a script that automatically optimize image with mozjpeg.

For those of you who don’t know about MozJPEG, this is a great photo optimizer tool from Mozilla. It is capable of compressing images very costly and still retains the image quality.

Script automatically optimize image

I wrote the script and put it on my gitlab repository. You can use the following command to download it to your computer.

wget -O

Or you can copy the script contents directly below and save the file

# Script author: Daniel Pham
# Script site:
# Script date: 19-03-2019
# Script ver: 1.0
# Script automatically optimize images with tool MozJPEG.

# Function optimize images
f_optimize () {
    # Create folder 'image' to save optimized images
    mkdir image

    # List all .jpg file in current folder
    ls | grep jpg > list.txt

    # Read file list.txt and optimize each image
    while read -r line
        /usr/bin/mozjpeg -optimize -quality 80 "$line" > image/"$line"
    done < list.txt

    # Remove file list.txt after finish optimize
    rm -f list.txt


# Main function
f_main () {

# Exit script

Explain my script

Recommended Reading: Script file location and execute script

Step 1: The script will create the image folder used to save the optimized images.

mkdir image

Step 2: List all JPG files in the current directory and save them as a list file.

ls | grep jpg > list.txt

And step 3: Use the mozjpeg command to optimize images.

while read -r line
    /usr/bin/mozjpeg -optimize -quality 80 "$line" > image/"$line"
done < list.txt

Use script

To use the script, follow the instructions below.

1. Download the script to your computer, name it

2. Put the script into the folder containing the .jpg image. For example, my photo folder is /Desktop/shutter.

3. Run the following command to execute the script.

$ bash

Example of using a script to optimize image

script automatically optimize images with mozjpeg
Script automatically optimize image with mozjpeg.

For example, in the shutter folder, there are 2 jpeg images of the following size.

104K Selection_001.jpg
116K Selection_002.jpg

Then I put the script into the folder shutter, opened the terminal and ran the execute command.


And this is the result of the optimized image size.

24K image/Selection_001.jpg
28K image/Selection_002.jpg


Mozjpeg is a great free tool for optimizing your photos. There is no problem when you optimize several images, but imagine if you have 1000 or even tens of thousands of photos. What you will do?

Now, this script will help you solve that problem very quickly. Hope it useful for you.

(This is an article from my old blog that has been inactive for a long time, I don’t want to throw it away so I will keep it and hope it helps someone).

0 0 votes
Article Rating

You may also like

Notify of
Inline Feedbacks
View all comments

DevOps Lite is a personal blog specializing in technology with main topics about DevOps, DevSecOps, SRE and System Administrator. Articles are shared for free and contributed to the community.



Subscribe my Newsletter for new blog posts. Stay updated from your inbox!

© 2021-2024 – All rights reserved.

Please write sources “” when using articles from this website.

This website uses cookies to improve your experience. We'll assume you're ok with this, but you can opt-out if you wish. Accept Read More

Would love your thoughts, please comment.x

Adblock Detected

Please support us by disabling your AdBlocker extension from your browsers for our website.