Bulk Optimize all JPEG, PNG, GIF images on Linux using jpegoptim and optipng (Ubuntu)

Keeping your images optimized is one of the keys to a fast website.

If you are hosting your website on a Linux machine, you may use jpegoptim and optipng to quickly optimize all images on your website. jpegoptim and optipng will strip all EXIF off the image files, which should make the image files much smaller.

First, you need to install jpegoptim and optipng on your machine. (Note: you will need root access to install the these). Just open your Linux terminal (or SSH to your server) and copy-paste these commands.

sudo apt install jpegoptim optipng -y

Next, navigate to your image directory. In my case my images reside in /var/www/site/wp-content/uploads.

cd /var/www/site/wp-content/uploads

Now run the following command to quickly optimize every single PNG, JPEG, and GIF image:

find -type f -name "*.jp*g*" -exec jpegoptim --strip-all {} \; && find -type f -name "*.png*" -exec optipng {} \; && find -type f -name "*.gif*" -exec optipng {} \;

This will scan and optimize all image files inside that folder and also any subfolders.

Ashik Emon

Ashik Emon

Ashik Ishtiaque Emon is a Google-certified Web Performance Expert and LPI-recognized Linux System Administrator. He travels and reads a lot. Reach him at [email protected].

More posts you might like: