17

This is pretty basic, I have a folder with several subfolders of JS files and i want to run Google's Clojure compiler on all of the files in those folders. The command to process a single file is as follows:

java -jar compiler.jar --js filename.js --js_output_file newfilename.js

How do I modify this to run on every JS file in my directory structure?

2 Answers 2

16

You can use find:

find . -name "*.js" -exec java -jar compiler.jar --js {} --js_output_file new{} \;
6
  • Thanks, and i can probably leverage this to rename everything because the original JS files needs to be overwritten
    – Mataniko
    Jun 27, 2013 at 8:00
  • In case the --js and --js_output_file need to differ I would make a two-line script that takes the filename as a parameter, write output to temporary file and mv the file to the original name.
    – Anthon
    Jun 27, 2013 at 8:07
  • If you want to avoid creating a separate script find -name '*.js' | while read jsfile; do java -jar compiler.jar --js "$jsfile" --js_output_file "new$jsfile" && mv "new$jsfile" "$jsfile"; done should do the trick. Jun 27, 2013 at 8:17
  • find -exec is great for simple commands. for complex ones find | xargs is much better and also it allows to run commands in parallel.
    – rush
    Jun 27, 2013 at 8:22
  • @rush maybe explain why xargs is better?
    – Mel Boyce
    Jun 27, 2013 at 12:39
11

You can also use a simple for loop, especially if the files are within a single directory (no subdirectories). It can be modified to work with subdirectories as well.

Without recursion:

for filename in ./*.js
do
    java -jar compiler.jar --js "${filename}" --js_output_file "new${filename}"
done

or as an equivalent one-liner:

for filename in ./*.js; do java -jar compiler.jar --js "${filename}" --js_output_file "new${filename}"; done

To recurse into subdirectories (requires GNU bash 4.0 or newer) (thanks @ChrisDown):

shopt -s globstar
for filename in ./**/*.js; do
    java -jar compiler.jar --js "${filename}" --js_output_file "new${filename}"
done
8
  • 1
    This does not recurse subdirectories as asked in the question. With bash4+, you can use globstar to do so.
    – Chris Down
    Jun 27, 2013 at 7:58
  • 1
    Yep, it's a good answer and great to know, but the other answer does exactly what i need it to.
    – Mataniko
    Jun 27, 2013 at 8:01
  • 1
    Ohh, I managed to completely miss the "several subfolders" part. Sorry. Leaving the answer around for completeness' sake, though.
    – user
    Jun 27, 2013 at 8:06
  • 3
    @rush Since ** can match an arbitrary level of recursion, **/*.js works: sprunge.us/PXQK
    – Chris Down
    Jun 27, 2013 at 9:02
  • 1
    It's generally a good idea to use for f in ./*.js; do or for f in ./**/*.js; do in order to protect yourself from filenames beginning with a - (I don't know if that's necessary in this case, but it's a good habit anyway).
    – evilsoup
    Jun 27, 2013 at 16:01

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.