CLI AI

Resize All Images in a Folder from the Command Line

2026-06-24

magick mogrify -resize 800x800 *.png resizes every image in a folder from the command line in one shot — but mogrify overwrites the source files while it does it, which is the trap nobody mentions. This also covers resizing a single image without warping it, the two-line loop that saves results into a separate folder instead of clobbering originals, and a real measurement showing that shrinking dimensions doesn't always shrink the file.

1. Resize a single image, keeping the aspect ratio

clai
$ clai shrink this image to 320 pixels on the longest side→ magick test1.png -resize 320x320 out1.jpg

320x320 is a bounding box, not a fixed size: ImageMagick keeps the aspect ratio and fits the image inside it. The 800x600 source came out at 320x240.

2. Smaller dimensions don't mean a smaller file

clai
$ clai compare the file size before and after→ identify -format '%wx%h %b\n' test1.png out1.jpg800x600 382B320x240 1188B

This is a real measurement, not a rule of thumb. A flat-color PNG weighed 382 bytes; after shrinking to 320x240 and converting to JPEG it came out at 1188 bytes — three times bigger. PNG compresses flat areas almost perfectly, JPEG isn't built for that. Always check the result instead of assuming it.

3. The whole folder at once, safely

clai
$ clai shrink every png in this folder to 800 pixels, don't touch the originals→ mkdir -p resized && for f in *.png; do magick "$f" -resize 800x800 "resized/$f"; done

A loop with a separate destination directory. The quotes around $f are mandatory — skip them and the first filename with a space breaks the whole batch.

4. mogrify: fast, but irreversible

clai
$ clai shrink every image in this folder in place→ magick mogrify -resize 800x800 *.png⚠ CAUTION — overwrites originals, no undo

mogrify exists specifically for in-place batch processing, and that's exactly why it's faster than a loop — it does nothing but overwrite in place. That's also why it's dangerous: once it runs, the originals are gone. Copy the folder before you run it.

5. Change the format for the whole folder

clai
$ clai turn every png in this folder into jpeg at quality 85→ magick mogrify -format jpg -quality 85 *.png

-format writes new files with the new extension next to the old ones — the source PNGs stay untouched. It's the one mogrify mode that overwrites nothing.

6. Shrink without stretching

clai
$ clai shrink only the images bigger than 1200 pixels→ magick mogrify -resize '1200x1200>' *.jpg

The > means "only if larger." Without it, small images get stretched up to 1200 and lose quality. The quotes keep the shell from reading > as output redirection — the single most common mistake with this flag.

Gotchas

  • mogrify overwrites the originals. Not convert, not magick file out, but mogrify specifically. The one exception is -format mode, which creates new files instead.
  • In ImageMagick 7 the command is magick. convert is kept for compatibility and may be missing entirely from newer installs. magick mogrify works on both generations.
  • -resize without a flag enlarges too. The modifiers: > shrink only, < enlarge only, ! ignore aspect ratio, ^ fill the box and crop.

Related questions

How do I crop to an exact size from the center? magick in.jpg -resize '400x400^' -gravity center -extent 400x400 out.jpg — fill the box first, then crop the excess.

How do I check dimensions without opening the file? identify -format '%wx%h %b\n' file.jpg. For a whole folder: identify -format '%wx%h %f\n' *.jpg.

Why is ImageMagick better than an online resizer? Files never leave the machine, there's no limit on how many you process, and one command handles a thousand images.

See also

CliAI writes the right ImageMagick flags for whatever you describe, resize and format conversion included. Install it in one line.