LinuxQuestions.org
Latest LQ Deal: Linux Power User Bundle
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 03-24-2010, 10:28 AM   #1
Euler2
LQ Newbie
 
Registered: Oct 2009
Posts: 20

Rep: Reputation: 0
Script to open all subdirectories of a single directory and then run a command (help)


Just as the title says, I am trying to write a very simple script that will go to every subdirectory of a single directory and run a command (lets call it make_ndx).

I know I can write this the long way with in a text document with something like:

cd /"the directory"/"the 1st subdirectory"
make_ndx
cd ..
cd "the 2nd subdirectory"
cd ..

Alternatively, I also tried:
for i in 'find /path/somemorepath -type d -mindepth 1'; do cd $i; make_ndx -f *.gro; done

which returns me with the error cd: find: no such file or directory.

But if I run the find command by itself to test if I am calling the right directories, it gives me the exactly the output I am looking for. Any ideas? Should I just write the find results to a file and loop through the contents of the file (which seems a little bit like overkill) or am I just making a simple typographical mistake and I am just not seeing it?

Any and all help is great.

Thanks.

Thanks for the help.
 
Old 03-24-2010, 10:34 AM   #2
schneidz
LQ Guru
 
Registered: May 2005
Location: boston, usa
Distribution: fc-15/ fc-20-live-usb/ aix
Posts: 5,027

Rep: Reputation: 845Reputation: 845Reputation: 845Reputation: 845Reputation: 845Reputation: 845Reputation: 845
backticks (not single quotes)"
Code:
for i in `find /path/somemorepath -type d -mindepth 1`; do cd $i; make_ndx -f *.gro; done
 
Old 03-24-2010, 10:38 AM   #3
catkin
LQ 5k Club
 
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Debian
Posts: 8,576
Blog Entries: 31

Rep: Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195
Try changing cd $i to echo cd $i to see what find is generating.

In case find gives directories with whitespace characters in the name it is safer to use cd "$i" than cd $i.
 
Old 03-24-2010, 10:40 AM   #4
catkin
LQ 5k Club
 
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Debian
Posts: 8,576
Blog Entries: 31

Rep: Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195
Quote:
Originally Posted by schneidz View Post
backticks (not single quotes)"
Code:
for i in `find /path/somemorepath -type d -mindepth 1`; do cd $i; make_ndx -f *.gro; done
Ha! As it says in Why is $(...) preferred over `...` (backticks)?: "The character ` is difficult to read with small or unusual fonts ...
The backtick is easily confused with a single quote
".
 
Old 03-24-2010, 02:28 PM   #5
Euler2
LQ Newbie
 
Registered: Oct 2009
Posts: 20

Original Poster
Rep: Reputation: 0
Thanks that fixed the problem.

As a follow-up, not that I have this working, I want to look into just one subdirectory whose name is repeated within the set of subdirectories...ok this is kind of convoluted so maybe I'll put it this way:

I am interested in looking at only a set of subdirectories with the same name (i.e. I have a set of directories that have another set of directories in them with the same name. Like a set of directories that are months, and then inside of them there are directories called days and I only want to look at day 30 of every month)

I try a simple script:

for i in `find /year/ -type d -mindepth 1`; do; if [ $i = "30"]; then; command_of_some_sort; fi; done

Any ideas?

Thanks for all of your help
 
Old 03-24-2010, 03:42 PM   #6
schneidz
LQ Guru
 
Registered: May 2005
Location: boston, usa
Distribution: fc-15/ fc-20-live-usb/ aix
Posts: 5,027

Rep: Reputation: 845Reputation: 845Reputation: 845Reputation: 845Reputation: 845Reputation: 845Reputation: 845
[untested]
Code:
find . -type d -mindepth  1 -name 30 -exec cd '{}' \;
 
Old 03-24-2010, 10:57 PM   #7
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,250

Rep: Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684
I would also add that unless make_ndx is creating / referencing files in the directory you are in, you do not
need to change into the directory.
 
Old 03-24-2010, 11:16 PM   #8
_Linux_Learner
Member
 
Registered: Feb 2010
Distribution: Ubuntu
Posts: 87

Rep: Reputation: 15
Hi Euler2,

Check out the following link.....

http://www.linuxquestions.org/questi...-error-789971/

Hope this helps
 
  


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
How to run a command in many subdirectories? mcgao07 Linux - Newbie 14 01-30-2009 07:58 AM
Using Bash, Find script files in a directory or subdirectories within... ray5_83 Programming 4 10-10-2008 08:42 PM
How can I run this script on all .sh in all subdirectories? jimdaworm Linux - Software 3 02-25-2008 07:46 PM
Run script on every file contained within a parent directory and its subdirectories General Programming 4 05-15-2006 03:12 AM
How do I run a command in script from a particular directory? davee Linux - Newbie 1 08-07-2003 04:22 AM


All times are GMT -5. The time now is 06:53 PM.

Main Menu
Advertisement
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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration