LinuxQuestions.org
Share your knowledge at the LQ Wiki.
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 01-27-2009, 10:07 PM   #1
mcgao07
LQ Newbie
 
Registered: May 2008
Posts: 15

Rep: Reputation: 0
Red face How to run a command in many subdirectories?


I have a quick question that seems easy to answer, but I have not figured it out...

I have a directory that contains many subdirectories: such as
p+0.1_8.6103_0
p+0.1_8.6103_10
p+0.1_8.6103_12
p-0.1_8.6103_15
p-0.1_8.6103_2
p-0.1_8.6103_4
p-0.1_8.6103_6
...

Now I want to do "qsub submission" ["submission" is a job submission script] in each subdirectory in bash. How should I do it? It must be related to "foreach" command, but I am in a big hurry...

Thanks a lot...

Michael

Last edited by mcgao07; 01-27-2009 at 10:30 PM. Reason: more details
 
Old 01-27-2009, 10:19 PM   #2
Tinkster
Moderator
 
Registered: Apr 2002
Location: in a fallen world
Distribution: slackware by choice, others too :} ... android.
Posts: 22,986
Blog Entries: 11

Rep: Reputation: 880Reputation: 880Reputation: 880Reputation: 880Reputation: 880Reputation: 880Reputation: 880
I don't know that the "queue job submission command" is, but
try something like
find /path/to/subdirs -execdir command ;


man find
/execdir

for details
 
Old 01-27-2009, 10:27 PM   #3
crabboy
Moderator
 
Registered: Feb 2001
Location: Atlanta, GA
Distribution: Slackware
Posts: 1,823

Rep: Reputation: 120Reputation: 120
Code:
#!/bin/sh

CWD=`pwd`
for i in `find . -type d -maxdepth 1`; do
   cd $i
   # do something here
   cd $CWD
done
 
Old 01-27-2009, 11:19 PM   #4
mcgao07
LQ Newbie
 
Registered: May 2008
Posts: 15

Original Poster
Rep: Reputation: 0
Quote:
Originally Posted by crabboy View Post
Code:
#!/bin/sh

CWD=`pwd`
for i in `find . -type d -maxdepth 1`; do
   cd $i
   # do something here
   cd $CWD
done
Crabboy,

Thank you, but there are problems.

1) there is error message:
find: warning: you have specified the -maxdepth option after a non-option argument -type, but options are not positional (-maxdepth affects tests specified before it as well as those specified after it). Please specify options before other arguments.

2) the script always find the parent directory. I want to exclude it from the list. How to do it?

Thank you.

Michael
 
Old 01-28-2009, 08:19 AM   #5
crabboy
Moderator
 
Registered: Feb 2001
Location: Atlanta, GA
Distribution: Slackware
Posts: 1,823

Rep: Reputation: 120Reputation: 120
1. Try switching the options. I don't see the message with my find, but the options work either way.

2. There is always the option of changing the . in find to be the full path to the parent directory. find will then produce a full path to the subdirectories, but you will still need supply the parent, as it is the reference to find the children.
Code:
#!/bin/sh

for i in `find /var/www -type d -maxdepth 1`; do
   cd $i
   # do something here
done
 
Old 01-28-2009, 11:50 AM   #6
mcgao07
LQ Newbie
 
Registered: May 2008
Posts: 15

Original Poster
Rep: Reputation: 0
Quote:
Originally Posted by crabboy View Post
1. Try switching the options. I don't see the message with my find, but the options work either way.

2. There is always the option of changing the . in find to be the full path to the parent directory. find will then produce a full path to the subdirectories, but you will still need supply the parent, as it is the reference to find the children.
Code:
#!/bin/sh

for i in `find /var/www -type d -maxdepth 1`; do
   cd $i
   # do something here
done
Hi,

Even I specified the path, it always find the parent directory first. Consequently, it also submits a job at the parent directory, which is not what I want.

I found the following works in my case since all my subdirectories start with "p", but not the most desirable:

#!/bin/bash

for i in `find . -name "p*"`; do
cd $i
qsub /nfs/home/mgao/script/atat_runstruct_vasp8
cd ../
done
 
Old 01-28-2009, 12:02 PM   #7
crabboy
Moderator
 
Registered: Feb 2001
Location: Atlanta, GA
Distribution: Slackware
Posts: 1,823

Rep: Reputation: 120Reputation: 120
I see now.

You can put this right after the for.
Code:
   if [ `pwd` = $i ]; then
      continue
   fi
 
Old 01-28-2009, 10:40 PM   #8
Tinkster
Moderator
 
Registered: Apr 2002
Location: in a fallen world
Distribution: slackware by choice, others too :} ... android.
Posts: 22,986
Blog Entries: 11

Rep: Reputation: 880Reputation: 880Reputation: 880Reputation: 880Reputation: 880Reputation: 880Reputation: 880
Hmmm ... not that crabboys solution is bad, but what's wrong
with
Code:
find -type d -mindepth 1 -maxdepth 1 -execdir qsub /nfs/home/mgao/script/atat_runstruct_vasp8 {} +
 
Old 01-28-2009, 11:12 PM   #9
mcgao07
LQ Newbie
 
Registered: May 2008
Posts: 15

Original Poster
Rep: Reputation: 0
Quote:
Originally Posted by crabboy View Post
I see now.

You can put this right after the for.
Code:
   if [ `pwd` = $i ]; then
      continue
   fi
Well, it did not work for some reason.

Michael
 
Old 01-28-2009, 11:15 PM   #10
mcgao07
LQ Newbie
 
Registered: May 2008
Posts: 15

Original Poster
Rep: Reputation: 0
Quote:
Originally Posted by Tinkster View Post
Hmmm ... not that crabboys solution is bad, but what's wrong
with
Code:
find -type d -mindepth 1 -maxdepth 1 -execdir qsub /nfs/home/mgao/script/atat_runstruct_vasp8 {} +

Well, it did not find the subdirectory, instead, it submitted the job n times (n=total # of subdirectories) in the parent directory.
 
Old 01-29-2009, 08:03 AM   #11
crabboy
Moderator
 
Registered: Feb 2001
Location: Atlanta, GA
Distribution: Slackware
Posts: 1,823

Rep: Reputation: 120Reputation: 120
The find on my slack machine has no execdir

try adding the mindepth as tink suggested:
Code:
#!/bin/sh

for i in `find /var/www -type d -maxdepth 1 -mindepth 1`; do
   cd $i
   # do something here
done
 
Old 01-29-2009, 10:23 AM   #12
schneidz
Senior Member
 
Registered: May 2005
Location: boston, usa
Distribution: fc-15/ fc-20-live-usb/ aix
Posts: 4,092

Rep: Reputation: 635Reputation: 635Reputation: 635Reputation: 635Reputation: 635Reputation: 635
how about this:
Code:
for dir in *
do
 cd $dir
 qsub submission # no idea what this is ?
 cd ..
done
should this be in the programming forum ?
 
Old 01-29-2009, 07:54 PM   #13
crabboy
Moderator
 
Registered: Feb 2001
Location: Atlanta, GA
Distribution: Slackware
Posts: 1,823

Rep: Reputation: 120Reputation: 120
schneidz, you script may not work all that well. If there are files in the directory, the 'for dir in *' will pick up the files as well as directories. The cd $dir will fail, on the files, and the script will cd .. regardless. This would put you up one directory and then the you'd attempt another cd $dir which would likely fail.
 
Old 01-29-2009, 11:21 PM   #14
schneidz
Senior Member
 
Registered: May 2005
Location: boston, usa
Distribution: fc-15/ fc-20-live-usb/ aix
Posts: 4,092

Rep: Reputation: 635Reputation: 635Reputation: 635Reputation: 635Reputation: 635Reputation: 635
^good feedback; i should've put a disclaimer that the above code was quick-and-dirty. the original poster said they needed an answer in a hurry so i didnt do any testing on it.

the original poster should tweak it to their hearts content anyways...

one could handle each exception in an if or test but i am too lazy rite now.

Last edited by schneidz; 01-29-2009 at 11:23 PM.
 
Old 01-30-2009, 06:58 AM   #15
mcgao07
LQ Newbie
 
Registered: May 2008
Posts: 15

Original Poster
Rep: Reputation: 0
Wink

Quote:
Originally Posted by crabboy View Post
The find on my slack machine has no execdir

try adding the mindepth as tink suggested:
Code:
#!/bin/sh

for i in `find /var/www -type d -maxdepth 1 -mindepth 1`; do
   cd $i
   # do something here
done
Yes, this works now. Thank you all for quick rescue.

Michael
 
  


Reply

Tags
bash, foreach


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
trying to fix xwindows or just run command line... changing run levels dave247 Debian 2 11-18-2008 06:11 PM
How can I run this script on all .sh in all subdirectories? jimdaworm Linux - Software 3 02-25-2008 06:46 PM
Command "find" misses some subdirectories vallond Linux - Enterprise 0 07-19-2007 04:55 AM
How do I perform a command on all files + subdirectories robertbrownell Linux - Newbie 2 02-11-2007 08:33 PM
Run script on every file contained within a parent directory and its subdirectories General Programming 4 05-15-2006 02:12 AM


All times are GMT -5. The time now is 12:15 PM.

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