Last active
August 21, 2023 08:50
-
-
Save iimos/7424025 to your computer and use it in GitHub Desktop.
Script for JPEG images optimization
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#! /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 |
Hello. Thx for the script first of all
i have the error runnig your script
./optimize-images.sh: 22: read: Illegal option -d
@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
Hello,
This script does not work when run inside directories with spaces and quotes in their name.
Please suggest for solve this.