LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   bash best practice : scripting actions to be done in all current subdirectories (https://www.linuxquestions.org/questions/programming-9/bash-best-practice-scripting-actions-to-be-done-in-all-current-subdirectories-743781/)

billywayne 07-29-2009 05:31 PM

bash best practice : scripting actions to be done in all current subdirectories
 
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

neonsignal 07-29-2009 09:11 PM

I don't know about best practice, but would this way be helpful?
Code:

find * -maxdepth 0 -type d -exec bash -c "cd \"{}\"; foo; bar" \;
The disadvantage is that it invokes a new shell for each subdirectory. The advantage is that for very large directories it does not have to
generate the whole list before doing anything.

The reason for the quotes around the 'cd' parameter is that some directories may have spaces in the name.

I was hoping to do something similar with the '-execdir' action on the find, but couldn't work out a way to make it only happen once on each subdirectory.

billywayne 07-31-2009 08:04 PM

thanks for the reply. i received an email when you replied two days ago, but strangely whenever I clicked on the link to view the thread, your reply wasn't showing up, until just now. weird.

i hadn't considered incorporating the commands directory into the 'find' command. It seems 'find' is more flexible than i had imagined.

thanks again.

neonsignal 07-31-2009 08:18 PM

Quote:

strangely whenever I clicked on the link to view the thread, your reply wasn't showing up
Yes, I have the same issue - I find on LQ that I always get cached copies of pages I have looked at recently, and have to press F5 to refresh.

billywayne 08-01-2009 07:42 PM

Quote:

Originally Posted by neonsignal (Post 3627226)
Yes, I have the same issue - I find on LQ that I always get cached copies of pages I have looked at recently, and have to press F5 to refresh.

Thanks for the hint!


All times are GMT -5. The time now is 08:57 PM.