Image optimisation – bulk

Tools used to optimise images within a folder/sub-folder: optipng, jpegoptim

To install these tools use the following commands:
Centos

yum install epel-release
yum update
yum install -y optipng jpegoptim

Debian/Ubuntu

apt install -y optipng jpegoptim

To use the the tools in a recursive way use the following bash script:

#!/bin/bash
################## USAGE ###########################
# ./optimiseImages.sh /full/path/to/folder OR
# ./optimiseImages.sh /full/path/to/folder > opt.log 
################## END USAGE #######################
folderPathOriginal=$1

function optiomiseImages(){
        folderPath=$1
        for file in "$folderPath"/*
        do
                if [ -d "${file}" ] ; then
                    optiomiseImages "$file"
                else
                        EXT="`echo ${file##*.} |tr '[a-z]' '[A-Z]'`"
                        if [ "$EXT" = JPG ]; then
                                jpegoptim -m 80 "$file"
                        else
                                if [ "$EXT" = JPEG ]; then
                                        jpegoptim -m 80 "$file"
                                else
                                        if [ "$EXT" = PNG ]; then
                                                optipng "$file"
                                        else
                                                echo "FILE $file could not be optimised"
                                        fi
                                fi
                        fi
                fi
        done
}
echo "---------------------FOLDER SIZE BEFORE OPTIMISE--------------"
du -h $folderPathOriginal
echo "---------------------BEGIN OPTIMISE--------------"
        optiomiseImages $folderPathOriginal
echo "---------------------END OPTIMISE--------------"
echo "---------------------FOLDER SIZE AFTER OPTIMISE--------------"
du -h $folderPathOriginal