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.

Amazon Logo Diesen Blog unterstützen?
Bestell dir doch etwas bei Amazon. Nutze diesen speziellen Link, es kostet dich nichts extra und für jeden Kauf darüber erhalte ich eine kleine Gutschrift. Danke!
✉ Marco Götze//

Kommentare

Formate: | Größe: Mb
Anmelden mitoder Benutzernamen eingeben

solariz
Not really possible. There danger for anything to mess up will be to big. But there is also a PHP tiny class you can use to compress inline code.
omar
hey nice script, now i have a question if you can help me.i need to use the compressor but with inline javascript inside a .html file, so basically compression everything inside a tag, any help I can do this with a terminal script like yours?
omar
hey nice script, now i have a question if you can help me.

i need to use the compressor but with inline javascript inside a .html file, so basically compression everything inside a tag, any help I can do this with a terminal script like yours?