How to Minify CSS and JS Files Using Linux Command Line

To minify CSS and JavaScript (JS) files using the Linux command line, you can utilize two popular tools: UglifyJS for JavaScript and UglifyCSS for CSS.

Minification is a process that helps you to reduce file sizes by removing unnecessary characters from source code, such as spaces, tabs, line breaks, and comments, without changing their functionality and can improve load times for web applications.

This article will guide you through the process of minifying CSS and JS files using the Linux command line interface (CLI) with UglifyJS and UglifyCSS tools.

Step 1: Installing Node.js and NPM in Linux

Before you start, make sure that you have Node.js and npm (Node Package Manager) installed on your Linux system.

node -v
npm -v

If they are not installed, you can install them on Debian-based distributions using the following commands.

sudo apt update
sudo apt install nodejs npm -y

On RHEL-based distributions, you can use:

sudo dnf update
sudo dnf install nodejs npm -y
Install NodeJS and NPM in Linux
Install NodeJS and NPM in Linux

Step 2: Installing UglifyJS and UglifyCSS

Once Node.js and npm are installed, you can use them to install UglifyJS and UglifyCSS, which are npm packages used for minifying JavaScript and CSS files, respectively.

sudo npm install -g uglify-js
sudo npm install -g uglifycss

After installation, check if UglifyJS and UglifyCSS are installed correctly by running:

uglifyjs -V
uglifycss -V
Check UglifyJS and UglifyCSS Version
Check UglifyJS and UglifyCSS Version

Step 3: Minifying JavaScript and CSS Files

To minify a JavaScript or CSS file, navigate to the directory containing the file you want to minify by running:

uglifyjs yourfile.js -o yourfile.min.js
uglifycss yourfile.css > yourfile.min.css

To minify multiple CSS and JS files in a directory, you can use a simple bash script that can significantly improve your website’s performance by reducing the file sizes.

Minifying Multiple JavaScript Files

Create a Bash script to minify all JS files.

nano minify_js.sh

Add the following script to the minify_js.sh file.

#!/bin/bash
for file in *.js; do
  if [ "$file" != "${file%.min.js}" ]; then
    echo "Skipping minified file: $file"
    continue
  fi
  uglifyjs "$file" -o "${file%.js}.min.js" --compress --mangle
  echo "Minified $file to ${file%.js}.min.js"
done

Make the script executable and run it.

chmod +x minify_js.sh
./minify_js.sh

This script will loop through all .js files in the directory, minify them using UglifyJS, and create new files with the .min.js extension.

Minifying Multiple JS Files
Minifying Multiple JS Files

If you don’t want to create new files, you can use the -o option in the bash script to overwrite the original files.

#!/bin/bash
for file in *.js; do
  if [ "$file" != "${file%.min.js}" ]; then
    echo "Skipping already minified file: $file"
    continue
  fi
  uglifyjs "$file" --compress --mangle -o "$file"
  echo "Minified $file"
done

Minifying Multiple CSS Files

Similarly, to minify all CSS files in a directory, create a Bash script.

nano minify_css.sh

Add the following script to the minify_css.sh file:

#!/bin/bash
for file in *.css; do
  if [ "$file" != "${file%.min.css}" ]; then
    echo "Skipping minified file: $file"
    continue
  fi
  uglifycss "$file" > "${file%.css}.min.css"
  echo "Minified $file to ${file%.css}.min.css"
done

Make the script executable and run it.

chmod +x minify_css.sh
./minify_css.sh

This script will loop through all .css files in the directory, minify them using UglifyCSS, and create new files with the .min.css extension.

Minifying Multiple CSS Files
Minifying Multiple CSS Files

If you don’t want to create new files, you can use the following bash script to overwrite the original files.

#!/bin/bash
for file in *.css; do
  if [ "$file" != "${file%.min.css}" ]; then
    echo "Skipping already minified file: $file"
    continue
  fi
  uglifycss "$file" > temp.css
  mv temp.css "$file"
  echo "Minified $file"
done
Conclusion

Minifying your CSS and JS files is a straightforward process using UglifyJS and UglifyCSS on the Linux CLI. This not only reduces the file sizes but also helps in improving the load times of your web pages.

Two small things before you go

If something didn’t work on your setup, leave a comment and tell me which distro and version you’re on. I read every one, and most of these articles got better because a reader pointed out what was missing.

And if it did work, share it. TecMint has stayed free for 14 years because people pass these articles around, and that traffic is what pays for writing the next one.

Leave a comment
Get two free Linux courses

Learn the Linux basics with two free courses and join 34,000+ readers getting practical TecMint tutorials and selected Pro TecMint articles every week.

Pro TecMint
Get lifetime access for $99

If you want to go beyond the free tutorials, Pro gives you 18 courses, 5 ebooks, and every future course I add. One payment, lifetime access, no renewals.

Ravi Saive
Ravi Saive
Administrator
About the author

I'm Ravi Saive, an award-winning entrepreneur and founder of several successful 5-figure online businesses, including TecMint.com, GeeksMint.com, UbuntuMint.com, and the premium learning hub Pro.Tecmint.com.

Got Something to Say? Join the Discussion...

Thank you for taking the time to share your thoughts with us. We appreciate your decision to leave a comment and value your contribution to the discussion. It's important to note that we moderate all comments in accordance with our comment policy to ensure a respectful and constructive conversation.

Rest assured that your email address will remain private and will not be published or shared with anyone. We prioritize the privacy and security of our users.