Large images means a large page size which means slow WordPress page load times for users. The most common culprit is image size or lack of compression. If you never had any image size restrictions or automatic resizing on your site, it can be extremely convenient to batch resize those images in Linux.

This guide shows you how to batch resize jpg and png files using Imagemagick on Ubuntu or Debian. If you want to install Imagemagick on CentOS see this guide.

Batch Resize Images using Linux Command Line and Imagemagick

First we need to install Imagemagick from the repository on Debian or Ubuntu

sudo apt-get update
sudo apt-get install imagemagick -y

We can use the identify command to get the width (%w) and height (%h)

identify -format "%wx%h" image.jpg

You will see the resolution, width is first then height.


You can resize the image if it is larger than the specified dimensions. This will automatically preserve the aspect ratio of the image too.

Note that this overwrites your original image!

convert image.jpg -resize 600x400\> image.jpg

Check the image was resized

identify -format "%wx%h" image.jpg

All good!


Now we can move on to batch resizing

Batch Resize Images with Linux with Imagemagick

Create the following script

mkdir -p ~/scripts
nano ~/scripts/

Paste the script below, remember to change the FOLDER variable!

FOLDER is the absolute path to your image folder

WIDTH is the max width and HEIGHT is the max height.

It will overwrite your original images so make sure you have a backup before running this!

#!/usr/bin/env bash

# absolute path to image folder
FOLDER="/var/www/path image"

# max height

# max width

#resize png or jpg to either height or width, keeps proportions using imagemagick
#find ${FOLDER} -iname '*.jpg' -o -iname '*.png' -exec convert \{} -verbose -resize $WIDTHx$HEIGHT\> \{} \;

#resize png to either height or width, keeps proportions using imagemagick
#find ${FOLDER} -iname '*.png' -exec convert \{} -verbose -resize $WIDTHx$HEIGHT\> \{} \;

#resize jpg only to either height or width, keeps proportions using imagemagick
find ${FOLDER} -iname '*.jpg' -exec convert \{} -verbose -resize $WIDTHx$HEIGHT\> \{} \;

# alternative
#mogrify -path ${FOLDER} -resize ${WIDTH}x${HEIGHT}% *.png -verbose

Ctrl+X, Y and Enter to save and exit.

Check the folder size

du -sh foldername

Check output size


Screen will ensure the batch command keeps on running even if your SSH session is terminated.

sudo apt-get install screen

Create a new screen session, press space or enter at the intro screen


Execute the script

bash ~/scripts/

Detach the screen with Ctrl+A and pressing D (detach).

You can use the top command and look for jpegoptim processes once in a while.

Reattach the screen like so

screen -r

Check the folder size again

du -sh foldername

You should see an improvement