Skip to content
Image optimization bash script
Branch: master
Clone or download
Latest commit ae9dc38 Apr 5, 2019
Type Name Latest commit message Commit time
Failed to load latest commit information.

Image optimization bash script


GitHub GitHub release GitHub last commit Github stars


  • jpegoptim for jpg optimization
  • optipng for png optimization
  • cwebp for WebP conversion

Debian/Ubuntu :

sudo apt install jpegoptim optipng webp -y

Centos 7 :

sudo yum install optipng jpegoptim libwebp-tools -y


  1. Clone the repository
git clone $HOME/.img-optimize
  1. Install the script

Method 1 : Add an alias in .bashrc

With this method img-optimize can only be used by the current user

echo "alias img-optimize=$HOME/.img-optimize/" >> $HOME/.bashrc
source $HOME/.bashrc

Method 2 : Copy the script in /usr/local/bin

With this method img-optimize can be used by all users

sudo cp $HOME/.img-optimize/ /usr/local/bin/img-optimize


Usage: img-optimize [options] <images path>
If images path is empty, img-optimize will use the current directory
       --jpg <images path> ..... optimize all jpg images
       --png <images path> ..... optimize all png images
       --webp <images path> ..... convert all images in webp
       --nowebp <images path> ..... optimize all png & jpg images
       --all <images path> ..... optimize all images (png + jpg + webp)
       -i, --interactive ... run img-optimize in interactive mode
 Other options :
       -h, --help, help ... displays this help information
  optimize all jpg images in /var/www/images
    img-optimize --jpg /var/www/images

Update the script

To update the script, just run :

git -C $HOME/.img-optimize pull

If you previously used the 2nd install method, you have to copy the script again to update it

sudo cp $HOME/.img-optimize/ /usr/local/bin/img-optimize -f

Setup daily cronjob

You copy the scripts to /etc/cron.daily :

cp $HOME/.img-optimize/crons/ /etc/cron.daily/jpg-png-cron
cp $HOME/.img-optimize/crons/ /etc/cron.daily/webp-cron

chmod +x /etc/cron.daily/jpg-png-cron
chmod +x /etc/cron.daily/webp-cron

Then just edit your websites path set with the variables sites at the beginning of the cron scripts.


Conversion process can take a while, you can use tmux to launch the script and be able to close your ssh connection without interrupting conversion. Then just use tmux attach to login back in your tmux session.


You can’t perform that action at this time.