• ARTICLES
SEARCH

BYTE SIZE TIPS / QUICK, HELPFUL TIPS

How-To Geek

How to Compress a Folder of Images Easily from the Command Line

If you’re dealing with a large number of images, you really don’t want to have to open each one in a photo editor and then save it out as an optimized web file. It’s much easier to do it in bulk, and if you are a keyboard ninja, you can do it easily with a single line.

If you are running Linux or Mac OS X, the bash shell makes it really easy to perform operations on more than one file at a time. If you are running Windows, we’d recommend installing Cygwin — and during the install process, pick the ImageMagick package. Otherwise you’ll have to download and install that separately. For Linux or Mac, you’ll want to install ImageMagick, which is pretty easy.

Now that you have that out of the way, you need to open a terminal window already set to the folder of images you want to manipulate. For cygwin, you’ll probably need to use the cd command and switch to the /cygdrive/c/ folder, like this:

cd /cygdrive/c/

Use the cd and ls commands to figure out where you are, and navigate into the folder that you want to manipulate. If you are a Linux user, you should already know how to do this.

Now you can finally run the command. Try this one on for size:

for f in *.jpg; do convert -quality 70 $f $f; done

The command uses the for operator to list through the list of files we defined as *.jpg, and then runs the convert command, which is actually ImageMagick, to change the quality of the JPG file down to 70.

If you wanted to compress just a single image, you could do it like this, adjusting the 70 to whatever level of compression you want — 50 would compress a lot more, but you will probably start to notice some serious quality degradation at that point.

convert -quality 70 image.jpg image.jpg

The first time you put the image name is to specify which file to open, and the second time is to specify which file to save it as, which in this example is the same. You could also just change the name of the output file if you wanted to save the existing files, though we’d recommend making a backup of all your images before you do anything.

Note: if you want to do the same thing on Windows without Cygwin, you can follow our updated guide that uses a batch file.

Lowell Heddings, better known online as the How-To Geek, spends all his free time bringing you fresh geekery on a daily basis. You can follow him on if you'd like.

  • Published 03/24/13

RECOMMENDED POSTS

Get Free Articles in Your Inbox!

Join 134,000 newsletter readers

Email:

Go check your email!