LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   Script to open all subdirectories of a single directory and then run a command (help) (https://www.linuxquestions.org/questions/linux-newbie-8/script-to-open-all-subdirectories-of-a-single-directory-and-then-run-a-command-help-797549/)

Euler2 03-24-2010 09:28 AM

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.

schneidz 03-24-2010 09:34 AM

backticks (not single quotes)"
Code:

for i in `find /path/somemorepath -type d -mindepth 1`; do cd $i; make_ndx -f *.gro; done

catkin 03-24-2010 09:38 AM

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.

catkin 03-24-2010 09:40 AM

Quote:

Originally Posted by schneidz (Post 3910385)
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
".

Euler2 03-24-2010 01:28 PM

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

schneidz 03-24-2010 02:42 PM

[untested]
Code:

find . -type d -mindepth  1 -name 30 -exec cd '{}' \;

grail 03-24-2010 09:57 PM

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.

_Linux_Learner 03-24-2010 10:16 PM

Hi Euler2,

Check out the following link.....

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

Hope this helps


All times are GMT -5. The time now is 04:16 PM.