Script automatically optimize image with mozjpeg

Share this post to your friends !

WriteBash - 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: Danie 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

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-image-with-mozjpeg Script automatically optimize image 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.

# bash

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.

If you liked this article, then please subscribe to our YouTube Channel for more video tutorials. You can also find us on Twitter and Facebook.

Share this post to your friends !
If you appreciate what we share in this blog, you can support us by:
  1. Stay connected to: Facebook | Twitter | Google Plus | YouTube
  2. Subscribe email to recieve new posts from us: Sign up now.
  3. Start your own blog with SSD VPS - Free Let's Encrypt SSL ($2.5/month).
  4. Become a Supporter - Make a contribution via PayPal.
  5. Support us by purchasing Ribbon Lite Child theme being using on this website.

We are thankful for your support.

single post bottom banner
«« »»

Got something to say? Join the discussion

Please keep in mind that all comments are subject to our Comment Policy. Your email address will not be published.
This site uses Akismet to reduce spam. Learn how your comment data is processed.