Skip to content

Instantly share code, notes, and snippets.

@ip413
Last active December 8, 2016 15:09
Show Gist options
  • Save ip413/651641d33b82e3c8bbbc6d09b024134d to your computer and use it in GitHub Desktop.
Save ip413/651641d33b82e3c8bbbc6d09b024134d to your computer and use it in GitHub Desktop.
Bash script for recursive optimization png files
#!/bin/bash
if [ -z "$1" ]
then echo "
Usage: script [directory] [change files]
Arguments:
directory - script take all jpg files from directory
change files - if true files will be changed, otherwise (default) then will not change any files (test run)
Requirements: optipng
"
else
dirName=$1
changeFiles=$2
startDirSize=$(du -s $dirName | cut -f 1)
find $dirName -type f -iname \*.png | while read file; do
echo "----------"
fileEscaped=$(echo $file | sed 's/ /\\ /g')
fileEscaped=$(echo $fileEscaped | sed 's/[(]/\\(/g;s/[)]/\\)/g')
if [[ $changeFiles == true ]]; then
simulationMode=""
else
simulationMode="-simulate "
fi
command="optipng -o1 -q $simulationMode \"$file\""
echo $command
eval $command
done;
echo "=========="
endSize=$(du -s $dirName | cut -f 1)
diffSize=$(($startDirSize - $endSize))
diffPercent=$(($(($diffSize*100))/$startDirSize));
echo "Saved $diffSize KB ($diffPercent%)"
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment