I'm not that versed in unix and I cannot have Java, so YUI Compressor does not apply, but I have this known Minify tool, which will get a minified version of a JS/CSS file from a specific URI /min/?f=/path/to/file.js.css

Which unix commands may I use, using such method, to minify all the js/css files on the public_html folder, replacing all js/css files by their minified versions?


After searching and implementing it, I give the answer here through a bash file.

I use the npm packages uglifyjs and uglifycss for compressing JS and CSS files respectively. I use command find to loop through those files. I assume js and css files are in a js/ and a css/ folder respectively.

#minification of JS files
find js/ -type f \
    -name "*.js" ! -name "*.min.*" ! -name "vfs_fonts*" \
    -exec echo {} \; \
    -exec uglifyjs -o {}.min {} \; \
    -exec rm {} \; \
    -exec mv {}.min {} \;

#minification of CSS files
find css/ -type f \
    -name "*.css" ! -name "*.min.*" \
    -exec echo {} \; \
    -exec uglifycss --output {}.min {} \; \
    -exec rm {} \; \
    -exec mv {}.min {} \;

This will minify all js and css files in respective js/ and css/ directories. If you want to exclude some particular folders or patterns within them use the option ! -name

| improve this answer | |
  • Is there a possibility to not rename but to have them like myScript.js and myScript.min.js ? – Christophe Ferreboeuf Nov 16 '18 at 8:43
  • 1
    @ChristopheFerreboeuf yes, remove the last two lines -exec rm {} \; and -exec mv {}.min {} \; – João Pimentel Ferreira Feb 4 '19 at 13:17
sudo apt-get install yui-compressor
yui-compressor finename.css > filename.min.css

Sources :

| improve this answer | |
  • I cannot use Yum – João Pimentel Ferreira Dec 24 '17 at 13:17
  • I'm sorry ! I knew this is not the perfect solution, but I through it could help others. I hope that since December 2016 you found a solution ! – FIfi Dec 24 '17 at 13:45
  • 2
    This solution uses yui-compressor, which uses Java. The question stated I cannot have Java, so YUI Compressor does not apply – MichielB Mar 5 '18 at 16:29

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.