Linux - NewbieThis 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
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
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
#!/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?
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
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
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.
^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.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.