Loop trough all folders recursively end execute make command when Makefile present
I really suck in bash scripting, so i'm hoping on your help guys. I need a script which will loop trough all folders, sub folders, sub sub folders, etc. inside folder /home/work and if there is present file Makefile then it should execute command make install
Folder structure is random, for example /home/work
- Dir 1 - - Dir 1.1 - - Dir 1.2 - - - Makefile - Dir 2 - - Makefile - Dir 3 - - Dir 3.1 - - Dir 3.2 - - - Dir 3.2.1 - - - Makefile - - MakeFileThis is what i have so far
for f in /home/work/*; do [ -d $f ] && cd "$f" && echo Entering into $f && make install done;If you need any additional information's please let me know and i will provide.
11 Answer
Using find:
find /home/work -type f -name Makefile -execdir make install \;find recursively searches /home/work for files (-type f) named Makefile (-name Makefile) and runs make install in the directory where the file was found (-execdir make install \;).
Alternatively, if you're using bash, enable ** (which is recursive):
shopt -s extglobThen do:
for f in /home/work/**/;
do [[ -f $f/Makefile ]] && echo Entering into "$f" && make -C "$f" install
doneWith a trailing slash after the wildcard, bash will only select directories, so you can eliminate that check. And make has an option to change directories before starting: -C. So, we can avoid the cd as well:
-C dir, --directory=dir Change to directory dir before reading the makefiles or doing anything else. If multiple -C options are specified, each is interpreted relative to the previous one: -C / -C etc is equivalent to -C /etc. This is typically used with recursive invocations of make. 0