Skip to content

Instantly share code, notes, and snippets.

@drygdryg
Last active July 1, 2024 14:39
Show Gist options
  • Save drygdryg/eb75fec65f63a98e2bfaec06e12c0034 to your computer and use it in GitHub Desktop.
Save drygdryg/eb75fec65f63a98e2bfaec06e12c0034 to your computer and use it in GitHub Desktop.
Find and optimize all PNG and JPG files in a directory using oxipng & jpegoptim
#!/bin/bash
directory=$1
pngfile_list=$(mktemp /tmp/oxipng_to_proceed.XXXXXX)
jpegfile_list=$(mktemp /tmp/jpegoptim_to_proceed.XXXXXX)
find "$directory" -maxdepth 1 -type f -exec file --mime-type {} + | grep 'image/png$' | cut -d ':' -f 1 > $pngfile_list
find "$directory" -maxdepth 1 -type f -exec file --mime-type {} + | grep 'image/jpeg$' | cut -d ':' -f 1 > $jpegfile_list
xargs --arg-file=$pngfile_list -d '\n' oxipng
xargs --arg-file=$jpegfile_list -d '\n' jpegoptim -m 80 -w `nproc --all`
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment