Loop through all folders and executing script

65,279

Solution 1

Use full path of your parent directory(in my case apps directory located in my home directory) and remove one extra command(cd ..)

for dir in ~/apps/*;
  do 
     [ -d "$dir" ] && cd "$dir" && echo "Entering into $dir and installing packages"
  done;

See screenshot: with cd .. command and using apps/*

enter image description here

See screenshot: without cd .. command and using ~/apps/*

enter image description here

Solution 2

You can use find along with exec for this propose. Your install.sh should be

#!/bin/bash
find ./apps -type d -exec echo Entering into {} and installing packages \; 

replace text after -exec with your command

for example

#!/bin/bash
find ./apps -type d -exec touch {}/test.txt  \;  

It will loop through app and all its sub-directories and will create a text.txt file

Share:
65,279

Related videos on Youtube

Anshad Vattapoyil
Author by

Anshad Vattapoyil

Works with Krews Limited, Abu Dhabi

Updated on September 18, 2022

Comments

  • Anshad Vattapoyil
    Anshad Vattapoyil almost 2 years

    I have a bash script install.sh in my current directory and I have a directory apps which contains multiple directories. I want to loop through these sub directories in app folder and execute some script. After executing script in first folder it should come back and enter into next folder. I have tried this but it's skipping one after another. I mean it's entering into all odd folders and not entering into even folders.

    Code in install.sh

    for f in apps/*;
      do 
         [ -d $f ] && cd "$f" && echo Entering into $f and installing packages
         cd ..
      done; 
    
    • fiatux
      fiatux over 9 years
      It looks like you're descending two levels down but only coming back up one level. Try changing cd .. to cd -
  • Anshad Vattapoyil
    Anshad Vattapoyil over 9 years
    Thanks it worked..curdir=$(pwd) for f in $curdir/apps/*; do [ -d $f ] && cd "$f" && echo Entering into $f and installing packages done;
  • αғsнιη
    αғsнιη over 9 years
    @devo Yes you can also use pwd to get the current directory. thanks ;)
  • banarun
    banarun over 9 years
    @KasiyA Can you explain why cd.. should be removed? shouldn't we go back after going into a directory?
  • αғsнιη
    αғsнιη over 9 years
    @banarun He(OP) can also use his own script but he need to use two level back cd command cd ../.. Because when he cd to $f directory now he is in /path/to/apps/1 directory and after cd .. he jumps to /path/to/apps directory (now curent directory is /path/to/apps) and when the next step of for-loop continue, the command looks the directory /path/to/apps/apps/* while that is not exist because ha doens't apps/apps/* directory and [ -d $f ] will return false. now cd .. command runs again and he jumps to /path/to ...
  • αғsнιη
    αғsнιη over 9 years
    ... and next step of for-loop runs and checks for directory /path/to/apps/* and he luck and he found directory in apps which is 3 (because he lost second index of for-loop for second directory) and this cause he just seen odd folders. And why I removed that(cd ..) and used full path of apps direcory. note that when you use a path in for-loop (apps/*), then this looks for directory apps/* in current working directory but when you use the full path, it just looks for dir's in that path not current directory.
  • αғsнιη
    αғsнιη over 9 years
    So the solution would be 1: using cd ../.. instead of cd .. (in OP case) or 2: using full path and removing cd ... right? :)