amazon media file explorer download upload index folder image licenses menu widget Play Pause profile-settings more dots-two-horizontal dots-two-vertical more-vertical pending google-plus hangouts facebook instagram whatsapp spotify telegram twitter vine renren rss youtube twitch vimeo flickr dribble behance deviantart 500px steam github soundcloud skype reddit linkedin lastfm delicious stackoverflow pinterest xing flattr foursquare yelp World

Bash Script For Optimizing ClassicPress & WordPress Images in Bulk

Out of frustration of the way different plugin optimizes images (which is super limited, and not effective; it has never been for me)in the WordPress world, I decided to create a script that does it on a server level.

This script would recursively optimize PNG or JPEG images using the convert command and the pngquant respectively.

Note: Tested on Ubuntu only

Another Note: Make Sure You Bakcup Your Website Before Optimizing

Step 1: Install Dependencies

The script uses convert and pngquant as its dependencies. convert is part of ImageMagick, and pngquant is a program by kornelski. You will need to install both first, do so, using:

sudo apt install imagemagick
sudo apt install pngquant

Step 2: Changing Command Path

The $path environment variable is a number of directories in which GNU/Linux will look for scripts and or programs.

GNU/Linux will only check for the executable in the path environment only when the full or relative path to the program is given, so, let’s configure the path, and this would be done via .bash_profile, chances are it’s not available in your shell. so, create and open using the following command:

touch ~/.bash_profile && nano .bash_profile

Add the following line in the file:

# User specific environment and startup programs


What the above command is saying, is Mr.Bash, please look into my home directory ($HOME) in a directory called (/bin), I have programs in there, and I want you to look into that folder whenever I run a program.

You know why this is useful, it is useful in the sense that the PATH variable prevents you from having to write out the entire path to a program or scripts every time you run it. Basically, it stores the shortcut to the program file.

Save, and Close the file. To reload changes immediately without logout, enter source ~/.profile

Verify it using echo $PATH

Lastly, let’s create a directory named bin in our user home directory, this would enable us to find the script by name without the file path:

test -d $HOME/bin && echo "Exist/Not Creating" || mkdir $HOME/bin

What the above command does is to check if a bin directory already existed in your user home directory, if it already exists, output “Exist/Not Creating“, and if does not already exist, create the folder.

Step 3: Install and Run The Script

create file in your bin directory:

nano $HOME/bin/

Copy and Paste the following script:

# Author: The_Devsrealm_Guy
# Website:
# Script to Optimize ClassicPress Images
# Last Edited: September 2020

# Check If The Process Isn't Running Already
if pidof -o %PPID -x "$0"; then
   exit 1

echo "|- A Program That Optimizes Your Classicpress Images! -|"
echo "Tip: For Agressive Optimization, You choose H, For Medium Optimization, Choose M, and L for Low Optimization"

read -p "Your ClassicPress Uploads Directory Path: " dir_name

read -p "Image format you want to optimize, png or jpg: " image_format

read -p "Choose Level of Optimization H, M or L: " image_optimization

# Check if variable isset

if [[ -z $dir_name || -z $image_optimization || -z $image_format ]]; then
echo "You Must Provide The Directory Path, Image Format, and Image Optimization Level "
exit 1

# Check If User Selected png, and begin png optimization
if [[ $image_format = "png" ]]; then
echo "Image Size Before Compression:" && find $dir_name .  -name '*.png' -exec du -ch {} + | grep total
case $image_optimization in

H) find $dir_name -regex ".*\.\(png\)" -exec pngquant --ext .png --force --quality=40 "{}" \; -exec echo "Optimized {}" \;
echo "Image Size After Compression:" && find $dir_name .  -name '*.png' -exec du -ch {} + | grep total

M) find $dir_name -regex ".*\.\(png\)" -exec pngquant --ext .png --force --quality=65 "{}" \; -exec echo "Optimized {}" \;
echo "Image Size After Compression:" && find $dir_name .  -name '*.png' -exec du -ch {} + | grep total

L) find $dir_name -regex ".*\.\(png\)" -exec pngquant --ext .png --force --quality=85 "{}" \; -exec echo "Optimized {}" \;
echo "Image Size After Compression:" && find $dir_name .  -name '*.png' -exec du -ch {} + | grep total

# Check If User Selected jpg, and begin jpg optimization
elif [[ $image_format = "jpg" ]]; then
echo "Image Size Before Compression:" && find $dir_name .  -name '*.jpg' -exec du -ch {} + | grep total
case $image_optimization in

H) find $dir_name -regex ".*\.\(jpg\|jpeg\)" -exec convert "{}" -quality 40 "{}" \; -exec echo "Optimized" "{}" \;
echo "Image Size After Compression:" && find $dir_name .  -name '*.jpg' -exec du -ch {} + | grep total

M) find $dir_name $dir_name -regex ".*\.\(jpg\|jpeg\)" -exec convert "{}" -quality 65 "{}" \; -exec echo "Optimized" "{}" \;
echo "Image Size After Compression:" && find $dir_name .  -name '*.jpg' -exec du -ch {} + | grep total

L) find $dir_name -regex ".*\.\(jpg\|jpeg\)" -exec convert "{}" -quality 85 "{}" \; -exec echo "Optimized" "{}" \;
echo "Image Size After Compression:" && find $dir_name .  -name '*.jpg' -exec du -ch {} + | grep total

echo "Image Format Not Recognized"   

# return

Save with Ctrl + x and then Y

Add permission bit: chmod +x $HOME/bin/

Now you can run the script from anywhere:

Here is an output:

Bash Script For Optimizing ClassicPress & WordPress Images in Bulk

Basically, you can choose three level of optimization, H for Higher compression, M for medium and L for low image optimization.

Point to your CP or WP image uploads directory, choose the format you want to compress, and choose the compression level.

It doesn't have to be pointed to your CP uploads directory though, point it to anywhere as long as it's a directory.

I'll update it with more features, for now, bye!


Related Post(s)

  • sed (Stream Editor) In Bash Scripting

    Another powerful filter in GNU/Linux is sed (stream editor), which is a program for performing basic editing tasks on the output of another program or on a file. sed or stream editor can be used to p

  • awk In Bash Scripting

    Up until now, we have covered a couple of text processing, from sed to grep to sort, and now we have the big daddy, which is awk. awk (which stands for Aho, Weinberger, and Kernighan - its creators)

  • (Bash Script) Automate Multiple Classicpress & Wordpress Installation + [Caching and SSL]

    If you are tired of using control panels (I am because they are mostly not secure, and most comes loaded with bundles of useless stacks), and want to do everything on the server level, I wrote a menu

  • Functions in Bash

    Functions are modular building blocks for creating powerful and modular scripts, using function makes it easy to isolate a code since it would be in a single building block, which would be isolated f

  • grep (Regular Expression) In Bash Scripting

    We recently discussed filters in bash scripting, in this guide we would look more into the practical usage of using a filter program, and an example of such a program is grep. grep, in its simplest f

  • Sort in Bash Scripting

    We recently just looked at grep in bash scripting, and another commonly used program in bash scripting is the program called sort. Sort is a simple filter that is used to sort lines of text from its