↑ ↓ select, Enter open, Esc close

Optimize PNG images losslessly with optipng

Caution: This command changes the system. Understand what it does before you run it.

Adjust the values, the command updates live
user@server
find /var/www/vhosts/example.com/httpdocs/wp-content/uploads -type f -iname '*.png' -exec optipng -o2 -strip all -preserve {} +

Tries various compression parameters and saves each PNG in the smallest variant without changing any pixels. The level ranges from 0 to 7, higher levels save a little more but take considerably longer. -strip all removes metadata, -preserve keeps timestamps and permissions.

Note: Install the optipng package if needed. The files are replaced in place, but the content stays pixel-identical.

Also searched as

  • lossless png compression linux
  • reduce png file size
  • run optipng on all images
  • make screenshots smaller

Convert images to AVIF recursively

$find /var/www/vhosts/example.com/httpdocs/wp-content/uploads -type f \( -iname '*.jpg' -o -iname '*.jpeg' -o -iname '*.png' \) -exec sh -c 'for f; do [ -e "$f.avif" ] || avifenc -s 6 "$f" "$f.avif" >/dev/null; done' sh {} +
cautionImages and performance

Convert images to WebP recursively

$find /var/www/vhosts/example.com/httpdocs/wp-content/uploads -type f \( -iname '*.jpg' -o -iname '*.jpeg' -o -iname '*.png' \) -exec sh -c 'for f; do [ -e "$f.webp" ] || cwebp -quiet -q 80 "$f" -o "$f.webp"; done' sh {} +
cautionImages and performance

Find large images in wp-content/uploads

$find /var/www/vhosts/example.com/httpdocs/wp-content/uploads -type f \( -iname '*.jpg' -o -iname '*.jpeg' -o -iname '*.png' -o -iname '*.gif' -o -iname '*.webp' \) -size +500k -printf '%s\t%p\n' | sort -rn | head -50 | numfmt --field=1 --to=iec
harmlessImages and performance

Optimize JPEG images losslessly with jpegoptim

$find /var/www/vhosts/example.com/httpdocs/wp-content/uploads -type f \( -iname '*.jpg' -o -iname '*.jpeg' \) -exec jpegoptim --strip-all --preserve {} +
cautionImages and performance

Shrink PNG images lossily with pngquant

$find /var/www/vhosts/example.com/httpdocs/wp-content/uploads -type f -iname '*.png' -exec pngquant --quality=65-85 --skip-if-larger --strip --force --ext .png {} +
destructiveImages and performance

Find images larger than a given pixel size

$find /var/www/vhosts/example.com/httpdocs/wp-content/uploads -type f \( -iname '*.jpg' -o -iname '*.jpeg' -o -iname '*.png' \) -exec identify -format '%w %h %i\n' {} + 2>/dev/null | awk -v max=2560 '$1 > max || $2 > max'
harmlessImages and performance

Read first, then run.

The commands on myline.de act directly on servers, files and databases. A wrong path or placeholder can delete data irreversibly or make a server unreachable.

  • All commands are provided without warranty and are not tested on every system.
  • Understand what a command does before running it, and check every placeholder.
  • Make a backup first and, if possible, try it on a test system.
  • You run commands at your own risk. Liability for damages is excluded to the extent permitted by law.