LinuxQuestions.org
Review your favorite Linux distribution.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices

Reply
 
Search this Thread
Old 09-14-2010, 10:51 AM   #1
jhavird
LQ Newbie
 
Registered: Sep 2010
Posts: 2

Rep: Reputation: 0
Executing a script in multiple subdirectories simultaneously


Hi,

I'm a newbie to shell scripting and have come up with a script (named "drop_3rd.sh") that I'd like to execute in several subdirectories that all have the same name, but I don't want to have to change into each subdirectory (there are hundreds) to execute the script.

All the subdirectories have the same name ("final_alignments"), but are located in parent directories with different names. All these parent directories are in the same "super parent" directory.

I've tried to do this using "for/do" and "find" but no luck so far.

Please help, as I'm sure this is a straightforward problem that can easily be solved by someone with more experience.

Thanks!
 
Click here to see the post LQ members have rated as the most helpful post in this thread.
Old 09-14-2010, 11:03 AM   #2
GrapefruiTgirl
Guru
 
Registered: Dec 2006
Location: underground
Distribution: Slackware64
Posts: 7,594

Rep: Reputation: 550Reputation: 550Reputation: 550Reputation: 550Reputation: 550Reputation: 550
I believe you're on the right track using `find` and a for/do loop. It'd be a good idea to show us what is the exact code you're currently using so that we might point out what you're doing wrong. Anyhow, here's the basic idea I'd use:

Code:
for location in $(find /path/to/super_parent_directory -type d -name "final_alignments"); do

 ( cd "$location"
   /path/to/script )

done;
So, it uses `find` to recursively search within the /path/to/super_parent_directory and locate directories (that's what the -type d is for) and it then (in a subshell) cd's into each final_alignments directory that it finds, and executes the script.

If this doesn't work, please show us (copy & paste) from your terminal how you've used this code, and what the results and error messages are.

Good luck!
 
1 members found this post helpful.
Old 09-14-2010, 04:50 PM   #3
dannybpng
Member
 
Registered: Sep 2003
Location: USA
Distribution: Fedora 20
Posts: 60

Rep: Reputation: 19
Let find do it all

Find can locate the directories, cd to each one, and run a command like this.

find /path/to/super_parent_directory -type d -name "final_alignments" -execdir /path/to/script \;

The \; is important as it keeps the shell from interpreting it and passes it on to the find command.
 
3 members found this post helpful.
Old 09-14-2010, 05:14 PM   #4
GrapefruiTgirl
Guru
 
Registered: Dec 2006
Location: underground
Distribution: Slackware64
Posts: 7,594

Rep: Reputation: 550Reputation: 550Reputation: 550Reputation: 550Reputation: 550Reputation: 550
@ danny,

thanks, that's something I hadn't thought of!
 
Old 09-15-2010, 08:54 AM   #5
jhavird
LQ Newbie
 
Registered: Sep 2010
Posts: 2

Original Poster
Rep: Reputation: 0
Smile Thanks!

Both these solutions seem to work, but I went with Danny's to save some time.

Thanks!

 
Old 09-15-2010, 01:37 PM   #6
Chirel
Member
 
Registered: Nov 2009
Posts: 55

Rep: Reputation: 19
You choosed the right one, because the first don't work if there is spaces on dir names.
 
Old 09-15-2010, 02:40 PM   #7
dannybpng
Member
 
Registered: Sep 2003
Location: USA
Distribution: Fedora 20
Posts: 60

Rep: Reputation: 19
FYI
The reason is that the results of the find command are "expanded" and placed on the "for" command line before each iteration, so the spaces get parsed as separate words. Another overhead problem is that the "( command )" has to create a subshell environment for each line. The "find" version is best. Here is a way you could do it if you'd like.

find /path/to/super_parent_directory -type d -name "final_alignments" | while read location; do pushd "$location">/dev/null; /path/to/script; popd>/dev/null; done;

In this example spaces in directory names get passed on OK. You always have to be careful of spaces in shell scripts. I always enclose variables being expanded in double quotes so it is expanded as one "word". The pushd and popd commands provide for cd'ing to a directory then poping back without having to start a subshell.
 
Old 09-15-2010, 03:52 PM   #8
Valery Reznic
ELF Statifier author
 
Registered: Oct 2007
Posts: 666

Rep: Reputation: 133Reputation: 133
OK, you already have a solution, but anyway.

What about:

Code:
for dir in /path_to/super_parent/*/final_alignment; do
   (
      cd $dir || exit
      /path/to/script || exit
   ) || exit
done
While find's -exec can be handy I not like it - even if one of the exec's failed, find's exit status still be 0
 
Old 09-15-2010, 06:40 PM   #9
grail
Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 7,541

Rep: Reputation: 1919Reputation: 1919Reputation: 1919Reputation: 1919Reputation: 1919Reputation: 1919Reputation: 1919Reputation: 1919Reputation: 1919Reputation: 1919Reputation: 1919
You can also get around the word splitting by using a while loop instead:
Code:
while read -r line
do
    cd "$line"
    /path/to/script || exit 1
done< <(find /path/to/super_parent_directory -type d -name "final_alignments")
Please note there is a space between < < after done
 
  


Reply


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
Executing multiple instances of a binary file from a single script? j_65_uk Linux - Newbie 3 07-02-2009 04:56 PM
Multiple Blade servers booting from multiple disk drives simultaneously NGC_cheryl Linux - Enterprise 0 11-26-2007 08:38 AM
executing multiple files (one after another) through shell script bilal_linux Programming 1 07-07-2007 01:31 AM
Bash script - executing a script through subdirectories bubkus_jones Programming 5 04-24-2006 05:05 PM
executing a script in multiple subdirectories jerryfreak Linux - Newbie 1 04-19-2005 08:34 PM


All times are GMT -5. The time now is 08:15 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