Blog Compress images from the terminal in bulk

Compress images from the terminal in bulk

2 min read

Compress images from the terminal in bulk
TL;DR Command-line tools compress and convert images in bulk. Loop over a folder to strip metadata and re-encode JPEG and PNG at a sensible quality, or convert everything to WebP or AVIF. It is faster than any GUI and easy to drop into a build step so nothing heavy ships by accident.

Images are usually the heaviest thing on a page, and the easiest weight to shed. You do not need a design app for it. Command-line tools compress and convert whole folders in seconds, which is both faster and easy to automate. Here is the practical version.

Compress a folder of JPEGs

Loop over the files and re-encode at a sensible quality, stripping metadata:

for f in *.jpg; do
  cjpeg -quality 80 "$f" > "optimized/$f"
done

A quality around 80 typically cuts the size substantially with no visible difference. Adjust to taste.

Squeeze PNGs

PNGs often carry a lot of avoidable weight. A lossy PNG optimiser handles a folder at once:

pngquant --quality=65-85 --ext .png --force *.png

That re-encodes each PNG within a quality range, dropping size hard while keeping it looking right.

Convert to modern formats

The bigger win for the web is switching to WebP or AVIF, which are much smaller than JPEG and PNG at similar quality:

for f in *.jpg *.png; do
  cwebp -q 80 "$f" -o "webp/${f%.*}.webp"
done

Serve those, with a fallback only if you truly need to support ancient clients.

Bake it into your build

The real payoff is automation. Drop the compression step into your build pipeline so every image is optimised on the way out, and no oversized asset ever ships because someone forgot. The command that processes a folder by hand is the same one your build runs; you just stop having to remember to run it.

The habit

Treat image weight as something you handle by default, not something you fix after the site feels slow. A one-line bulk command, run on your assets or wired into the build, keeps the heaviest part of your pages light without any manual fiddling. It is one of the cheapest performance wins there is, and the terminal makes it effortless to apply to everything at once.

FAQ

Why compress images from the command line?

Speed and repeatability. You can process a whole folder in one command, script it, and drop it into a build so images are optimised automatically. A GUI is fine for one image; the terminal wins the moment you have many or want it automated.

Should I convert images to WebP or AVIF?

Usually yes for the web. WebP and AVIF are significantly smaller than JPEG and PNG at similar quality, and they are widely supported. Keep a fallback if you must support very old clients, but for most sites modern formats are a large, easy win.

Will compression hurt image quality?

Not noticeably if you pick a sensible quality level. Most images can drop a large fraction of their size at a quality setting no one can tell apart from the original. The trick is choosing a level that is aggressive but still looks right.