Batch compress images from the command line

For a folder of ten thousand images, a browser is the wrong tool. Here are the commands that do it properly, with the destructive ones flagged.

The tools

ImageMagick
The generalist. Resize, convert and compress anything.
cwebp
Google's WebP encoder. Part of the libwebp tools.
MozJPEG (cjpeg)
Smaller JPEGs than any browser produces.
oxipng
Fast lossless PNG optimisation.
jpegoptim
Lossless JPEG optimisation and metadata stripping.
ExifTool
Metadata inspection and removal.
The warning
mogrify overwrites in place. Copy the folder first.

Working safely

  1. Step 1: Copy the folder before you start

    Several of these commands overwrite in place with no undo. Work on a copy until the settings are right, every time — this is the single most expensive mistake in this area.

  2. Step 2: Test on one file

    Run the command against a single image, inspect the result at display size, and check the file size. Only then run it against the folder.

  3. Step 3: Resize before compressing

    As always, dimensions dominate. Cap the longest edge first and the quality setting has far less work to do.

  4. Step 4: Then automate it

    Once the command is right, move it into a build step or a scheduled job so nobody has to remember to run it.

The commands

ImageMagick — the generalist

mogrify edits in place; magick writes new files. Prefer the second until you are sure.

# Resize and compress into a new folder (safe)
mkdir -p out
magick mogrify -path out -resize 1600x1600\> -quality 80 -strip *.jpg

# The same, overwriting the originals (destructive — copy first)
magick mogrify -resize 1600x1600\> -quality 80 -strip *.jpg

The > after the dimensions means "only shrink, never enlarge", which is almost always what you want. -strip removes metadata including EXIF and colour profiles.

cwebp — converting a folder to WebP

# macOS / Linux
for f in *.jpg; do cwebp -q 78 "$f" -o "${f%.jpg}.webp"; done

# With a resize in the same pass
for f in *.jpg; do cwebp -q 78 -resize 1600 0 "$f" -o "${f%.jpg}.webp"; done

Passing 0 for the height preserves the aspect ratio. For lossless WebP from PNG sources, use -lossless instead of -q.

MozJPEG — the smallest JPEGs

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

Typically 5–15% smaller than the same quality from a browser or standard libjpeg, and the output is an ordinary JPEG that opens everywhere. If you are building a pipeline, this is usually the encoder to use.

PNG and metadata

# Lossless PNG optimisation, in place
oxipng -o 4 --strip safe *.png

# Lossless JPEG optimisation and metadata removal, in place
jpegoptim --strip-all *.jpg

# Remove metadata only, leaving the image untouched
exiftool -all= -overwrite_original *.jpg

All three of these are genuinely lossless on the image data. ExifTool in particular is the right tool when you want the metadata gone without re-encoding the pixels at all.

Windows

ImageMagick's syntax is the same; the loop is not.

# PowerShell
Get-ChildItem *.jpg | ForEach-Object {
  magick $_.FullName -resize 1600x1600">" -quality 80 -strip "out\$($_.Name)"
}

Note the quoting around >, which PowerShell otherwise treats as redirection. WSL is often simpler if you are following examples written for a Unix shell.

When the command line is the wrong answer

For a handful of images it is slower than dropping them into a browser, and for anything containing an identity document there is no privacy argument — a local tool and a client-side browser tool are equally private.

Where it wins decisively is scale, repeatability and encoder quality: thousands of files, the exact same settings every time, and access to MozJPEG and AVIF encoders that browsers do not expose.

Do it here instead

These run in your browser on any device, so nothing is uploaded and there is nothing to install.

  • Bulk compressor

    One setting across a whole folder, processed in parallel, downloaded as a ZIP.

    Open the tool
  • JPG to WebP

    Typically 25–35% smaller at matched quality. The standard web win.

    Open the tool
  • Resize image

    By pixels or percentage, aspect ratio locked, across a whole batch.

    Open the tool
  • Compress JPEG

    Shrink JPG and JPEG photographs with a quality slider or an exact target size.

    Open the tool

Questions

Frequently asked questions

Every answer below is present in the page source, expanded, so it can be read without opening anything.

Does mogrify overwrite my originals?

Yes, in place and with no undo, unless you pass -path to send output elsewhere. Copy the folder before running it. This is the most common and most expensive mistake with ImageMagick.

Which encoder produces the smallest JPEGs?

MozJPEG, typically 5–15% below standard libjpeg at matched visual quality, with output that is an entirely ordinary JPEG. jpegli is a newer alternative worth testing against your own content.

How do I convert a folder to WebP?

Loop cwebp over the files: for f in *.jpg; do cwebp -q 78 "$f" -o "${f%.jpg}.webp"; done. Add -resize 1600 0 to cap the width in the same pass, where 0 preserves the aspect ratio.

What does -strip actually remove?

EXIF, colour profiles, comments and embedded thumbnails. That saves bytes and removes GPS coordinates, but it also removes the ICC profile — convert to sRGB first if the image is in a wider colour space, or the colours will shift.

Is the command line better than a browser tool?

For scale, repeatability and encoder quality, yes. For a handful of files it is slower, and it offers no privacy advantage over a client-side browser tool, since neither uploads anything.

Can I compress losslessly from the command line?

Yes. oxipng and optipng for PNG, jpegoptim for JPEG, and ExifTool to strip metadata without touching the pixels at all. All are genuinely lossless on the image data.

From the blog

Related reading

More background from the blog.

  • Practical Guides

    How to Compress Images for a Website Without Wrecking Them

    Resize before you compress. Measured on one photograph: half the width at quality 80 produced a 16.4 KB file, while full width at quality 40 produced 20.6 KB and looked far worse. The order of operations matters more than the settings.

    8 min read

  • Image Compression

    JPEG Quality Settings Explained: What the Number Actually Means

    Quality 80 is not 80% of anything. It is an index into a quantisation table. Measured on one photograph: dropping from 100 to 90 removes 77% of the file and 4.2 dB of fidelity; dropping from 40 to 30 removes 17% and costs almost as much.

    8 min read

  • Image Formats

    WebP vs JPEG: Which Should You Use, and When JPEG Still Wins

    WebP produced a 27.0 KB file against MozJPEG's 41.1 KB on the same photograph at slightly higher measured fidelity — 34% smaller. It also produced a larger file than JPEG on random noise. The advantage is real and content-dependent.

    9 min read