Skip to content

Instantly share code, notes, and snippets.

@taji-taji
Last active August 29, 2015 14:10
Show Gist options
  • Save taji-taji/a25ef1f9d306993338f3 to your computer and use it in GitHub Desktop.
Save taji-taji/a25ef1f9d306993338f3 to your computer and use it in GitHub Desktop.
jpegtran whole directory
#! /bin/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
for file in $(find $DIR -name '*.optimized'); 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