Skip to content

Instantly share code, notes, and snippets.

@iimos
Last active August 21, 2023 08:50
Show Gist options
  • Star 14 You must be signed in to star a gist
  • Fork 7 You must be signed in to fork a gist
  • Save iimos/7424025 to your computer and use it in GitHub Desktop.
Save iimos/7424025 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
@swatinuna
Copy link

Hello,

This script does not work when run inside directories with spaces and quotes in their name.

Please suggest for solve this.

@tazman
Copy link

tazman commented Nov 23, 2016

Hello. Thx for the script first of all
i have the error runnig your script
./optimize-images.sh: 22: read: Illegal option -d

@mordka
Copy link

mordka commented Jul 4, 2017

@tazman you may try changing #! /bin/sh to #!/bin/bash

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment