# Minify multiple Javascript files in a folder with UglifyJS

**Published:** August 17, 2017
**Tags:** javascript, jekyll

**Summary:** Minify every JavaScript file in a folder with a short UglifyJS plus Bash loop, no Grunt setup required.


---

To minify multiple Javascript files, you can use [Grunt](https://gruntjs.com/). However, there is a much easier way to archive it using [UglifyJS](http://lisperator.net/uglifyjs/) and Bashscript.

### Step 1: Install UglifyJS

```
npm install -g uglify-js
```

### Step 2: Write some Bash script code to find and minify all js files in a folder

```bash
for file in path/to/js/folder/*.js; do
    uglifyjs "$file" --stats -c -m  -o "$file"
    echo minified: "$file"
done
```

### Step 3: Run script

Save script in Step 2 to a file called 'minify-js.sh' and run it using Terminal:

```bash
sudo chmod +x minify-js.sh
./minify-js.sh
```

Modify this code a litte and you can use it with [cleancss](https://www.npmjs.com/package/clean-css-cli) or other minifier.

This code help me much in deploying [my webiste](https://vietanhdev.com) (a static site built with Jekyll). Hope it help someone else.

