My shell is bash and I need to write a script that runs a specific software for each input data in the several folders. For example:

gd2p.pl -i /home/WORKDIR/folder1/input1.o

Let's say that I have 20 folders in WORKDIR and each folder includes input files. How can I write the script which run the above codes for each folder and each input files automatically?

  • 2
    yes it's correct. – deepblue_86 Apr 6 '15 at 8:50

You can try this code.

for file in */*; do           # Loop trough any file in any folder
    if [ -f "$file" ]; then   # If the current element is a file
        gd2p.pl -i "$file"    # Run the program
    fi                        # End if
done                          # End loop
  • @muru: Thanks by your edit. I ever forget the quotes. – Helio Apr 6 '15 at 8:59

My solution would be to use find:

find /home/WORKDIR/ -name "input*.o" -exec gd2p.pl -i {} \;

That gives you (virtually) unlimited depth in sub-folder structure.

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

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