Batch Compress Images from Command Line
In this article
This guide shows you how to compress multiple images at once using free command-line tools. You'll learn two practical methods—one using ImageMagick (cross-platform) and one using the built-in tools on macOS and Linux. By the end, you can shrink a folder of photos or screenshots without opening a single image editor.
What You'll Need
- A terminal (Command Prompt on Windows, Terminal on macOS/Linux).
- ImageMagick installed (for the primary method). On macOS:
brew install imagemagick. On Ubuntu/Debian:sudo apt install imagemagick. On Windows: download from imagemagick.org and install. - Basic comfort with navigating folders in the terminal (
cdandls).
Step-by-Step
- Open your terminal and navigate to the folder containing your images. For example:
cd ~/Pictures/vacation. Verify you're in the right place withls(ordiron Windows). - Create a new folder for the compressed output to keep originals untouched. Run:
mkdir compressed. - Compress all JPEG images in the current folder with ImageMagick's
mogrifycommand. The simplest form is:mogrify -path compressed -quality 85 *.jpg. This creates compressed versions of all .jpg files in thecompressedfolder, with quality set to 85% (a good balance between size and quality). - For PNG files, use a different approach because PNG is lossless. You can reduce file size by reducing colors or using
pngquant(install separately). With ImageMagick alone, you can do:mogrify -path compressed -define png:compression-level=9 *.png. This maximizes compression but may not shrink files dramatically. - Resize images as you compress to save even more space. For example, to limit the width to 1920 pixels:
mogrify -path compressed -resize 1920x -quality 85 *.jpg. The-resize 1920xkeeps the aspect ratio and only sets the width. - Use a loop for more control (especially on macOS/Linux). If you need to keep original filenames and process each file individually, use a for loop:
for img in *.jpg; do convert "$img" -quality 85 "compressed/$img"; done. This is slower but lets you add extra options per file. - Compress all images in subfolders as well by using
findwithmogrify. On macOS/Linux:find . -name "*.jpg" -exec mogrify -path compressed -quality 85 {} \;. This processes every JPEG in the current directory and all subdirectories, placing results in thecompressedfolder (which must exist). - Check the results by comparing file sizes. Use
ls -lh compressedto see the new sizes. Open a few images to verify quality is acceptable.
Common Mistakes
- Forgetting the
-pathoption: Without it,mogrifyoverwrites your original files. Always use-pathto specify an output folder. - Using
convertfor batch jobs:convertprocesses one file at a time. For multiple files, usemogrifyor a loop.mogrifyis designed for batch operations. - Not quoting filenames with spaces: If your files have spaces (e.g., "my photo.jpg"), you must quote them in loops:
"$img". Unquoted, the shell splits the name into multiple arguments. - Setting quality too low: Quality 50 or below can cause visible artifacts, especially on text or gradients. Start at 80-85 and adjust.
- Ignoring the output folder existence:
mogrify -pathwill fail if the target folder doesn't exist. Create it first withmkdir.
Tips
- Test on a few images first before compressing an entire library. Run the command on 3-4 files and inspect the quality.
- Use a quality of 80-85 for photos—it's often visually indistinguishable from the original but significantly smaller.
- For web use, consider resizing to the maximum display size. A 4000px wide photo is overkill for a blog post. Resize to 1920px or 2560px to save megabytes.
- Strip metadata to save extra bytes: add
-stripto remove EXIF data (camera info, GPS, etc.). Example:mogrify -path compressed -strip -quality 85 *.jpg. - Use
jpegoptimon Linux for even better JPEG compression. It's a dedicated tool that often produces smaller files than ImageMagick at the same quality. - Convert PNG to JPEG when appropriate: If a PNG is a photo (not a screenshot or graphic with transparency), converting to JPEG at quality 85 can drastically reduce size. Use
mogrify -path compressed -format jpg -quality 85 *.png.
Troubleshooting
- "command not found": ImageMagick is not installed. Install it via your package manager (brew, apt, or the Windows installer). Alternatively, use
sipson macOS (see FAQ). - "mogrify: unable to open image": The file might be corrupt or not an image. Check the file type with
file filename.jpg. - Output folder not created:
mogrify -pathdoes not create the folder. Runmkdir compressedfirst. - Files are not compressed: If the output size is similar or larger, the image may already be highly compressed, or you're using a high quality setting. Try lowering quality or resizing.
- Permission denied: You might not have write access to the output folder. Use a folder in your home directory or run with
sudo(be careful). - Loop with spaces fails: Ensure you use
"$img"in the loop, not$img. Also, usefor img in *.jpgwhich handles spaces correctly in the glob.
Frequently Asked Questions
Can I compress images without installing anything?
Yes. On macOS, use the built-in sips tool: sips -s formatOptions 85 -s format jpeg *.jpg --out compressed/. On Linux, you might have convert from ImageMagick pre-installed, but not always.
What's the difference between convert and mogrify?
convert processes a single image and outputs a new file. mogrify is designed for batch processing and can overwrite originals (unless you use -path). For batch tasks, use mogrify.
How do I compress images to a specific file size?
ImageMagick doesn't directly target a file size. You can estimate by adjusting quality and resizing. For precise control, use a tool like jpegoptim with the --size option, or use a GUI app.
Will compression reduce image quality?
Yes, JPEG compression is lossy—higher compression (lower quality) reduces file size but introduces artifacts. PNG compression is lossless, so quality remains identical, but file size reduction is limited.
Can I compress images in subfolders and keep the folder structure?
Yes, but it's more complex. You can use a shell script with find to recreate the directory tree. A simpler approach is to use zip with compression, but that doesn't compress the images themselves.
Is there a way to compress all images regardless of extension?
Yes, you can use wildcards like * or *.png *.jpg *.jpeg. Be careful: mogrify will try to process any file, so specify the extensions you want.