Skip to content

Instantly share code, notes, and snippets.

@pavel-arapov
Created August 11, 2014 13:58
Show Gist options
  • Select an option

  • Save pavel-arapov/860727bff0f739aa2bea to your computer and use it in GitHub Desktop.

Select an option

Save pavel-arapov/860727bff0f739aa2bea to your computer and use it in GitHub Desktop.
Create gzip version of static files
#!/bin/bash
FILETYPES=( "*.html" "*.css" "*.js" "*.xml" "*.txt" )
DIRECTORIES="/path/to/site/dir/"
MIN_SIZE=512
for currentDir in $DIRECTORIES;
do
for f in "${FILETYPES[@]}";
do
files="$(find $currentDir -iname "$f")";
echo "$files" | while read file;
do
PLAINFILE=$file;
GZIPPEDFILE=$file.gz;
if [[ -e "$GZIPPEDFILE" ]]; then
if [[ `stat --printf=%Y "$PLAINFILE"` -gt `stat --printf=%Y "$GZIPPEDFILE"` ]]; then
#echo .gz is older, updating $GZIPPEDFILE;
gzip -2 -f -c "$PLAINFILE" > "$GZIPPEDFILE";
fi;
if [[ `stat --printf=%s "$PLAINFILE"` -le $MIN_SIZE ]]; then
echo Uncompressed size is less than minimum "("$(stat –printf=%s "$PLAINFILE")")", removing
rm -f "$GZIPPEDFILE";
fi;
elif [[ `stat --printf=%s "$PLAINFILE"` -gt $MIN_SIZE ]]; then
echo Creating .gz "for" "$PLAINFILE";
gzip -2 -c "$PLAINFILE" > "$GZIPPEDFILE";
fi;
done
done
done
exit
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment