Search code examples
linuxgrepfindqsub

Run a qsub command in all subdirectories


I am using Centos on a HPC to run my code. I typically have a folder that contains a run_calc File, which is what I want to run as:

qsub run_calc

Now I want to write a script "submit_all.sh" that submits all run_calc files in all the subfolders in their current directory and not from the from a parent folder where I runt the submit_all.sh script.

I found similar questions posted here Solution and here Solution2 that seems to be a partial answer to this question. I am not confident just submitting scripts until I found a solution which is why I ask:

In the second link I found this solution:

for i in {1..1000}; do
    cd "$i"
    qsub submit.sh
    cd ..
done

were "i" was a list of folders with the names 1-100. Is it somehow possible to use find to create a list of all the subdirectories and path it to the for loop? How would i deal with subsubdirectories? Would I be able to change the cd .. statement such that I always go back to the parent folder directly in that case?

I fond this solution here: Solution

#!/bin/sh

for i in `find /var/www -type d -maxdepth 1 -mindepth 1`; do
   cd $i
   # do something here
done

But I do not understand what is going on? Is it possible to change the above script to the only dive into folders containing a run_calc File and also include subsubdirectries?

Thank you in advance


Solution

  • Assuming that you are using bash as your shell:

    $ cat ./test.sh
    #!/bin/bash
    
    IFS=$'\n' 
    while read -r fname ; 
    do 
        pushd $(dirname "${fname}") > /dev/null
        qsub run_calc 
        popd > /dev/null 
    done < <(find . -type f -name 'run_calc')
    

    find . -type f -name 'run_calc' finds all paths to file run_calc inside the current directory and its subdirectories. This is input for the while loop.

    pushd, popd are bash specific, and adds in or pops out of directory stack.