user2044576
user2044576

Reputation: 31

Execute same command in multiple subdirectories

I've written a fortran script that I would like to run in over 200 directories. The directories are all named case_1, case_2, etc. I wanted to know if there is a command I can run so that this script is executed in all of these sub-directories. I do not want to execute this command 200 times.

Thanks!

Upvotes: 1

Views: 536

Answers (2)

SethMMorton
SethMMorton

Reputation: 48885

Sounds like you want something like this:

for dir in case_*
do
    cd $dir
    /path/to/fortran/command
    cd .. # <- EDIT: This brings you back to the original directory
done

This should get you started.

Upvotes: 0

glenn jackman
glenn jackman

Reputation: 247250

GNU parallel might be useful here. Untested:

parallel 'cd {} && yourProgram' ::: case_*

Upvotes: 2

Related Questions