Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

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?

share|improve this question

2 Answers 2

up vote 7 down vote accepted

You can use find:

find . -name "*.js" -exec java -jar compiler.jar --js {} --js_output_file new{} \;
share|improve this answer
    
Thanks, and i can probably leverage this to rename everything because the original JS files needs to be overwritten –  Mataniko Jun 27 '13 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 '13 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. –  David Baggerman Jun 27 '13 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 '13 at 8:22
    
@rush maybe explain why xargs is better? –  Mel Boyce Jun 27 '13 at 12:39

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
share|improve this answer
1  
This does not recurse subdirectories as asked in the question. With bash4+, you can use globstar to do so. –  Chris Down Jun 27 '13 at 7:58
    
Yep, it's a good answer and great to know, but the other answer does exactly what i need it to. –  Mataniko Jun 27 '13 at 8:01
    
Ohh, I managed to completely miss the "several subfolders" part. Sorry. Leaving the answer around for completeness' sake, though. –  Michael Kjörling Jun 27 '13 at 8:06
2  
@rush Since ** can match an arbitrary level of recursion, **/*.js works: sprunge.us/PXQK –  Chris Down Jun 27 '13 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 '13 at 16:01

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.