Minify and Compress CSS & Javascript Files At a Linux/Unix Shell Prompt

For CSS files:

  1. CSSMin: A simple command line tool to minify CSS files. To install it, run the following command:
npm install -g cssmin

To minify a CSS file, run the following command:

cssmin input.css > output.min.css
  1. YUI Compressor: Another popular command line tool to minify CSS files. To install it, run the following command:
wget https://github.com/yui/yuicompressor/releases/download/v2.4.8/yuicompressor-2.4.8.jar

To minify a CSS file, run the following command:

java -jar yuicompressor-2.4.8.jar input.css -o output.min.css

For JavaScript files:

  1. UglifyJS: A JavaScript parser, minifier, compressor and beautifier toolkit. To install it, run the following command:
npm install -g uglify-js

To minify a JavaScript file, run the following command:

uglifyjs input.js -o output.min.js
  1. Closure Compiler: A JavaScript optimizer developed by Google. To install it, run the following command:
wget https://dl.google.com/closure-compiler/compiler-latest.zip
unzip compiler-latest.zip

To minify a JavaScript file, run the following command:

java -jar closure-compiler.jar --js input.js --js_output_file output.min.js

Here is an example of how to minify and compress a file called style.css:

cleancss style.css > style.min.css

And here is an example of how to minify and compress a file called script.js:

uglifyjs script.js -o script.min.js

Note: You may need to install the uglifyjs and cleancss packages first using a package manager like npm or yarn.

Leave a Comment