My last scripting related article has long been gone. So today just a short one, nothing special but a handy tool. Today most pages use CSS Stylesheets to define the layout and design time to time those CSS files are getting huge, bigger than the actual webpage HTML. There are several ways to “minify” / “compress” them. A Browser dont need the comments or linebreaks to interpret a code. But that`s all for the basics. One of the most used Tools is the YUI-compressor wich is a JAVA based JS & CSS Compression tool. I use a small bash script to process all my webpage CSS files with this compressor. It simple compress all CSS files in the current folder and output the saved bytes.

#!/bin/sh
echo Compressing CSS Files...
saved=0
for f in `find -name "*.css" -not -name "*.min.css"`;
do
	target=${f%.*}.min.css
	echo "\t- "$f to $target
	FILESIZE=$(stat -c%s "$f")
	yui-compressor --type css --nomunge -o $target $f
	FILESIZEC=$(stat -c%s "$target")
	diff=$(($FILESIZE - $FILESIZEC))
	saved=$(($saved + $diff))
	echo "\t  $diff bytes saved"
done
echo "Done ! Total saved: $saved bytes"
chown www-data.www-data *.min.css



This is how the output looks like for CSS of this page:
use YUI Compressor to compress a whole folder

Free to use, improvements welcomed.

✉ MG// CEST

Follow Icon
Don’t miss out and subscribe by email:
Don't worry! NO Spam and FREE; Receive a summarizing email for new posts, easy to unsubscribe at any time.
← Other Blog Posts