Skip to content

Instantly share code, notes, and snippets.

@dkesberg
Forked from iimos/optimize-images.sh
Created April 7, 2017 07:43
Show Gist options
  • Save dkesberg/94a033bea111af91472eb2fa36cf7ea7 to your computer and use it in GitHub Desktop.
Save dkesberg/94a033bea111af91472eb2fa36cf7ea7 to your computer and use it in GitHub Desktop.
Script for JPEG images optimization
#! /bin/sh
# Usage 1:
# optimize-images.sh /images/dir
#
# Usage 2:
# cd /images/dir
# optimize-images.sh
EXTENSIONS="jpe?g"
if [ -z "$1" ]; then
DIR="`pwd`"
else
DIR="$1"
fi
# Optimize JPEG images
find "$DIR" -regextype posix-egrep -regex ".*\.($EXTENSIONS)\$" -type f | xargs -I{} jpegtran -optimize -progressive -outfile "{}.optimized" "{}"
# Rename xxx.jpg.optimized -> xxx.jpg
find "$DIR" -name '*.optimized' -print0 | while read -d $'\0' file; do
chown $(stat -c "%U:%G" "${file%.optimized}") "$file"
chmod $(stat -c "%a" "${file%.optimized}") "$file"
mv -f "$file" "${file%.optimized}";
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment