Running a command on all files within a directory
Hi All,
I have a bunch of css and js files that I want to minify.These reside in different directories. I want to minify them using the YUI compressor.The compressor is a jar file, and it is possible to compress a file using the following command: java -jar yuicompressor-2.4.2.jar -v -o button.css button.css here the source file is button.css and I am just overwriting the current contents with the minified contents. I was wondering if anyone can tell me if given a parent directory how I can recurse through all the sub directories - minifying all the css/js files that I find by applying the above command. Thanks in advance for any suggestions. |
You'll be wanting the find command:
Code:
find /parent/directory -iname "button.css" -exec java -jar yuicompressor-2.4.2.jar -v -o button.css "{}" \; Code:
find /parent/directory -iname "button.css" -exec echo java -jar yuicompressor-2.4.2.jar -v -o button.css "{}" \; As I said, be careful, because as soon as you whip out the echo, it'll actually start doing things to files. I'd highly recommend having a backup before you start this. |
Assuming all the css/js files have extensions .css and .js then try
Code:
/usr/bin/find <top level directory name> -name '*.css' -o -name '*.js' -exec echo <your command> {} \; EDIT: pwc101 got there first while I was typing :) |
Recursion can be performed by the command find. A simple way to do that is a loop over the result of the find command, that is a list of all .css and .js files inside the source directory tree. For example:
Code:
while read file Edit: beaten by pwc101 and catkin! Too slow this time! ;) |
Quote:
|
Using -iname is a habit I've developed, though probably not always a great idea. I should add -type f more frequently as I rarely work with directories in this manner. Still, the solution is likely to be a combination of the three :)
|
Thank you very much for all your suggestions.Its very encouraging when you get 3 great replies to your very first post.
I found all the suggestions given to be very useful,but I finally went with colucix's suggestion: while read file do echo java -jar yuicompressor-2.4.2.jar -v -o "$file" "$file" done < <(find /path/to/parent/dir -type f \( -name \*.css -o -name \*.js \)) it was exactly what I wanted |
All times are GMT -5. The time now is 12:13 AM. |