Search code examples
bashjar

Bash run a jar file on text files in a folder


Theres a jar file that alphabetizes a series of names in .txt file. I have lots of these files in folders. the normal use of the .jar is:

java -jar names.jar -o output/alphabetized/ names/names-05-04-20.txt

How could you run the jar file on the txt files in a folder?

EDIT: names.jar alphabetizes the contents of the files passed to it, the goal being to alphabetize the contents of the files in the folder.


Solution

  • Use a shell for loop:

    for F in names/*.txt; do java -jar names.jar -o output/alphabetized/ "${F}"; done
    

    You can see what this is going to do by adding echo before java to show the command instead of running it:

    $ for F in names/*.txt; do echo java -jar names.jar -o output/alphabetized/ "${F}"; done
    java -jar names.jar -o output/alphabetized/ names/actors.txt
    java -jar names.jar -o output/alphabetized/ names/apple-products.txt
    java -jar names.jar -o output/alphabetized/ names/fruit.txt
    java -jar names.jar -o output/alphabetized/ names/planets.txt
    java -jar names.jar -o output/alphabetized/ names/presidents.txt