Hello!
I've been writing bash scripts which perform actions in all the subdirectories in a current directory.
I've implemented this by executing a for loop which recurses over every subdirectory. My question is, how exactly should I obtain the names of the current subdirecties?
What I've been doing is this ...
Code:
for SUBDIR in `ls -d /*` ; do
cd $SUBDIR
foo
bar
done
But this sometimes works, sometimes doesn't and I'm sure this isn't best practice.
In the meantime, I've changed to using
Code:
for SUBDIR in `find . -maxdepth 1 -mindepth 1 -type d` ; do
cd $SUBDIR
foo
bar
done
Actually, I simply created an alias for this particular find command:
Code:
alias subdirs='find . -maxdepth 1 -mindepth 1 -type d'
and then write the for loop with
Code:
for SUBDIR in `subdirs` ; do ...
But I want to know what is considered best practice for recursively entering subdirectories and executing commands from within them. Please keep in mind that I really do need to actually cd into each directory to perform the actions from within the subdirectory.
Any and all advice appreciated.
Thanks!
billywayne