1

I'm somewhat new to Ubuntu and I installed it to do a small project. I extracted about 30 tar archives with packages in them and now I have a lot of directories, each containing the source code and a configure file.

How can I quickly loop through all the subdirectories and run the configure shell script in each one?

So far, I have tried creating a shell script (called i.sh) that loops through the folders and runs the configure script, but to no avail. Is there any way it can be corrected? And how would I execute it?

find . -type d | while read d; do
    ./configure
    make
    sudo make install
done

I executed it by going into the terminal and running sudo bash ./i.sh.

Thank you!

1
  • 1
    You did notcd into the directory before running configure. Remember to cd back to the original directory at the end of the loop.
    – doneal24
    Commented Apr 30, 2020 at 16:40

1 Answer 1

1

To iterate over all directories in the current directory, use a simpler loop:

for dir in ./*/; do
   # more here
done

The pattern ./*/ would match subdirectories in the current directory.

Using find would most likely do the wrong thing here as it would find subdirectories in the unpacked tar archives.

For each directory, $dir, you want to cd into that directory and do the build:

for dir in ./*/; do
    ( cd "$dir" && ./configure && make && sudo make install )
done

The parenthesis makes the commands run in a subshell. The change of the directory inside the subshell will not be reflected outside of the subshell, so there's never a need to "cd back".

I've strung together the commands with && ("and"). This means that if one command fails, the later commands would not be run at all.

You would not execute this with sudo, as the only step that might possibly need root privileges is the make install step, which is already prefixed by sudo.

Note that this would install all the projects under /usr/local by default. To install in another installation prefix, use e.g. --prefix=$HOME/local with configure (which means you would not need sudo at all, since you in already have write permissions in your home directory).

1
  • Thank you! This is just what I needed.
    – atulw
    Commented Apr 30, 2020 at 21:16

You must log in to answer this question.

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