Compile Commit
Compile Commit

Reputation: 443

Execute multiple shell scripts of same name under several sub-directories

I am using a shell script from a parent directory to call multiple other shell scripts, all having the same name, but under separate subdirectories. Let me elaborate below:

Parent directory: Script deploy_modules.sh
SubDir A: deploy.sh
SubDir B: deploy.sh
SubDir C: deploy.sh
....
SubDir Z: deploy.sh

The script deploy_modules.sh calls each of the sub-directory's deploy.sh sequentially by entering that directory, calling the script, then exiting and entering the next one.

Is there a way to sequentially execute all the deploy.sh files in a single line command? I need the command to be generic enough to automatically include any newly added sub-directory with another deploy.sh, and not give error if one or more of the sub-directories are removed.

Upvotes: 1

Views: 833

Answers (2)

user1934428
user1934428

Reputation: 22217

Something like

find Parentdir -maxdepth 1 -name deploy.sh -exec {} \;

Omit the maxdepth option, if you want to include deploy scripts further down in your tree as well.

In case you need to execute the deployment script from inside the directory it is in (though you don't mention this in your question), use -execdir instead of -exec.

Upvotes: 0

Ted Lyngmo
Ted Lyngmo

Reputation: 117298

This would find all deploy.sh scripts in the subdirectories to wherever the current directory is - and execute them.

Without doing cd down into the subdirectories:

for dep in */deploy.sh; do "$dep"; done

Doing cd down into each subdirectory:

for dep in */deploy.sh; do (cd "$(dirname "$dep")"; ./deploy.sh) done

Upvotes: 3

Related Questions