I have several directories of the form:


I want to execute all of the qsub.sh in each of these subdirectories, but when I tried to do

sh my_dir/subdir*/qsub.sh

it seems to only run the qsub.sh for a single subdirectory. Why does it do this?

  • How do you want them to run? One after the other, or in parallel?
    – terdon
    Nov 28, 2023 at 10:57

2 Answers 2


The reason your command does not work is because the shell will execute the first match, and the rest of the matches will be passed as parameters to that first script.

You need to iterate the directories and run each script individually.

For example

for script in my_dir/subdir*/qsub.sh ; do
  sh "${script}"

As bxm already said: The shell is expanding your command to

sh my_dir/subdirA/qsub.sh my_dir/subdirB/qsub.sh my_dir/subdirC/qsub.sh

which runs my_dir/subdirA/qsub.s with the arguments my_dir/subdirB/qsub.sh my_dir/subdirC/qsub.sh

An alternative to for loop could be:

find my_dir -name qsub.sh -exec sh '{}' \;

Extended tips for newbies: Make all your scripts executable (chmod +x) and starting with a proper shebang (e.g. #!/usr/bin/env sh). Then you can enable a script by making it executable and you can also use other scripting languages like python, perl or even binaries without modifying your execution logic.

Then run find:

find my_dir -type f -executable -name qsub.sh -exec '{}' \;

Check man find for Options to control how to deal with symbolic links or search depth.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .