Bash script for batch running OptiPNG

Optimize all your images with a single command:

$ find . -name '*.png' | xargs optipng -nc -nb -o7 -full

What is OptiPNG?

OptiPNG is a PNG optimizer tool. If you want your website to be faster, you should consider optimizing images.

When I ran the Google PageSpeed tool towards my website, I got a score of 81/100 for Desktop, 61/100 for Mobile. Pretty low, I thought. So I decided to go through the “Consider Fixing” issues, and the first one was - guess what? - optimize images.

Properly formatting and compressing images can save many bytes of data.

The thing I did immediately next was reading the article that Google suggested: it’s a nice reading on Google Developers about optimizing images. The first recommendation is: Use an image compressor. OptiPNG is one of them.

On the OptiPNG site I realized it works only for one image at a time, so I had the need to write a script that runs the optimizer for all images at the same time.