1

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
 - - MakeFile

This 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.

1
  • you can have a one liner using the find command and its exec parameter. $ find /home/work -name Makefile -exec 'cd basename {}; make; cd $OLDPWD'
    – Iyad K
    May 31, 2016 at 9:47

1 Answer 1

4

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 extglob

Then do:

for f in /home/work/**/;
do
     [[ -f $f/Makefile ]] && echo Entering into "$f" && make -C "$f" install
done

With 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

You must log in to answer this question.

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