Batch Process with YUI Compressor

The [other day][] I covered how to use YUI Compressor like any old command line for those of us not overly used to working with java classes. This is great for working with single files but I wanted something to iterate through a directory and compress all files of a certain type. So here is a little command line tool for doing that. (Refer to the [previous post][other day] for more background info)


# Batch file processor for YUI Compressor.
# Save as 'yuibatch' in /usr/local/bin/java with chmod a+x.

# REQUIRED: /usr/local/bin/java/yuicompressor.jar

# USAGE: yuibatch [type]

# Alias to compressor class
# 	(assumes $PATH and $CLASSPATH contain /usr/local/bin/java)
alias yui='java'

# Alternatively
# alias yui='java -jar /usr/local/bin/java/yuicompressor.jar'

for file in `find . -name "*.$1"`
echo "Compressing $file …"
yui --type $1 -o $file $file

Save this file as ‘yuibatch’ in /usr/local/bin/java with chmod a+x. The script assumes that both `$PATH` and `$CLASSPATH` contain `/usr/local/bin/java` and also assumes that you have yuicompressor.jar installed in `/usr/local/bin/java`. Though you can forgo most of those assumptions and use the alternative alias pointed at where ever your `yuicompressor.jar` exists.

To use it, navigate to the folder in question and type the command `yuibatch [type]` where type is the file type you wish to compress, either `css` or `js`. For example:

user:~$ cd ~/some/folder

user:~some/folder$ yuibatch css
  Compressing ./somefile.css …
  Compressing ./subfolder/anotherfile.css …

user:~/some/folder$ yuibatch js
  Compressing ./somefile.js …
  Compressing ./subfolder/anotherfile.js …

[other day]: “Simplified CLI use of YUO Compressor”


Adam Merrifield


Leave a Reply