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?
Answers:
Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.
Method 1
You can use find:
find . -name "*.js" -exec java -jar compiler.jar --js {} --js_output_file new{} ;
Method 2
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
All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0