LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
Search this Thread
Old 07-29-2009, 05:31 PM   #1
billywayne
LQ Newbie
 
Registered: May 2009
Posts: 15

Rep: Reputation: 0
Question 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
 
Old 07-29-2009, 09:11 PM   #2
neonsignal
Senior Member
 
Registered: Jan 2005
Location: Melbourne, Australia
Distribution: Debian Wheezy (Fluxbox WM)
Posts: 1,363
Blog Entries: 52

Rep: Reputation: 353Reputation: 353Reputation: 353Reputation: 353
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.

Last edited by neonsignal; 07-29-2009 at 09:15 PM.
 
Old 07-31-2009, 08:04 PM   #3
billywayne
LQ Newbie
 
Registered: May 2009
Posts: 15

Original Poster
Rep: Reputation: 0
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.
 
Old 07-31-2009, 08:18 PM   #4
neonsignal
Senior Member
 
Registered: Jan 2005
Location: Melbourne, Australia
Distribution: Debian Wheezy (Fluxbox WM)
Posts: 1,363
Blog Entries: 52

Rep: Reputation: 353Reputation: 353Reputation: 353Reputation: 353
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.
 
Old 08-01-2009, 07:42 PM   #5
billywayne
LQ Newbie
 
Registered: May 2009
Posts: 15

Original Poster
Rep: Reputation: 0
Quote:
Originally Posted by neonsignal View Post
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!
 
  


Reply

Tags
bash, recursion, subdirectories


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Find files in current directory, not subdirectories SmurfGGM Linux - Newbie 1 10-09-2008 12:14 PM
scripting, number of subdirectories in a subdirectory disruptive Programming 2 06-04-2008 04:33 PM
bash scripting help want to execute external function on current item. Benanzo Linux - Software 3 02-03-2007 02:45 PM
Bash Expression: Current Directory & All Subdirectories Below gmcauley Programming 2 01-01-2007 02:36 PM
Bash Scripting (subdirectories) bendeco13 Linux - General 4 10-07-2004 12:24 AM


All times are GMT -5. The time now is 12:07 AM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration