Search code examples
bashshellubunturecursiongnu-findutils

How can I recursively go to every folders and execute shell script with the same name?


I have this directory.

.
├── animation
│   ├── animation-events
│   │   ├── app.js
│   │   ├── app.mustache.json
│   │   ├── create_view.sh
│   │   └── assets
│   │       └── dummy_character_sprite.png
│   └── change-frame
│       ├── app.js
│       ├── app.mustache.json
│       ├── create_view.sh
│       └── assets
│           └── dummy_character_sprite.png
├── app.css
├── app.mustache
├── bunch_of_functions.js
├── decorators.js
├── create_all_views.sh
└── displaying-a-static-image
    ├── app.js
    ├── app.mustache.json
    ├── create_view.sh
    └── assets
        └── piplup.png

I want for create_all_views.sh to execute all create_view.sh in the children directory. How can I achieve such thing?


Solution

  • As you are in Ubuntu, you have the GNU implementation of find, which has the -execdir option, and you can do like this:

    find path/to/dir -name create_view.sh -execdir ./create_view.sh \;
    

    That is, for each create_view.sh file it finds in the directory tree, it will execute ./create_view.sh in the directory of that file.