LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   Using foreach to run same command in several folders (https://www.linuxquestions.org/questions/linux-newbie-8/using-foreach-to-run-same-command-in-several-folders-854668/)

anhedonia 01-06-2011 01:31 PM

Using foreach to run same command in several folders
 
I am trying to run the same script on a xml file in several different subfolders. I assume I need to use the foreach command.

I start in the parent folder. I want to then go into each subfolder and run a script.

I have the following:

xmlscript */xmlfile

That runs the script on each file, but it is done at the parent level and the output (which has the same name for each instance) simply copies over the previous output.

Is there way where I get the name of each directory, cd to that directory, and then run the script from within that directory?

thanks in advance

acid_kewpie 01-06-2011 02:00 PM

what language are you using? I recall "foreach" existing in perl and php...

anhedonia 01-06-2011 02:06 PM

I am using linux. I assumed foreach based on some google searches. Not married to it.

acid_kewpie 01-06-2011 02:08 PM

erm... married? What? Do you have a language you're using or not? It's pretty odd to pick a command without first picking a language that can support it... :confused:

colucix 01-06-2011 02:12 PM

Quote:

Originally Posted by anhedonia (Post 4215603)
I am using linux.

That's not a language. Maybe you mean a shell like bash, ksh, tcsh and so on. You can determine it by means of
Code:

echo $SHELL
foreach is an iteration available in C-shells. If you're running a C-shell like tcsh, maybe something like this is what you're looking for:
Code:

#!/bin/tcsh
foreach dir ( */ )
  cd $dir
  /path/to/xmlscript xmlfile
  cd ..
end

Otherwise take a look at the find command and the -execdir predicate.

crts 01-06-2011 02:27 PM

Hi,

if you want/can use bash then you could try something like:
Code:

find /path/to/parent/ -type d -exec /path/to/xmlscript '{}'/xmlfile \;
UNTESTED!

Or maybe even better yet:
Code:

find /path/to/parent/ -type f -name xmlfile -exec /path/to/xmlscript '{}' \;

anhedonia 01-06-2011 03:41 PM

Sorry, I am using bash.

crts,

Your script is still saving the output in the source directory of where I am running the bash script. I will keep playing it, but is there a way to define the output folder?

thanks again

colucix 01-06-2011 03:45 PM

The -execdir option of find does exactly what you're looking for:
Code:

-execdir command {} +
  Like -exec, but the specified command is run from the subdirectory containing the matched
  file, which is not normally the directory in which you started find. This a much more
  secure method for invoking commands, as it avoids race conditions during  resolution  of
  the paths to the matched files.


anhedonia 01-06-2011 04:50 PM

-execdir is getting me closer

I am in a directory called 'books' which is where I am running the script. Within that directory, I have two sub-directories, book1 and book2. Each of those directories has a file called a book.xml that I want to run a transform script against.

I run:

find */ -name book.xml -execdir /path/to/xml/script '{}' \;

I assume that in literal terms that is saying in the folders below, look for any file names book.xml and run the xmlscript against that book.xml in the directory in which it was found.
I would want the output of my conversion of book1/book.xml to end up in the book1 directory and the output of the conversion of book2/book.xml to end up in the book2 directory.

The script as I have it runs the conversion against book1/book.xml and saves the output in the book1 directory. Perfect. However, instead of then going to book2/book.xml. It once again runs the conversion against book1/book.xml. Any ideas as to what I may be missing.

thanks again

crts 01-06-2011 05:10 PM

Your command looks 'strange' and I haven't seen it being used this way.
Anyway, can you first try to run the command
Code:

find . -type f -name 'book.xml' -execdir /path/to/xml/script '{}' \;
You will have to run it from inside the parent directory. Alternatively you can give the full path to the parent folder
Code:

find /path/to/parent -type f -name 'book.xml' -execdir /path/to/xml/script '{}' \;
Then you can run it from anywhere.

colucix 01-06-2011 05:17 PM

Apparently I cannot see any reason for this to happen. What is the output of the find command without the -execdir option?

anhedonia 01-06-2011 05:31 PM

Did a little playing around and got the following.

Here is my exact script as run from the parent folder:

find . -type f -name book.xml -execdir xsltproc ../../../test/docbook-xsl-test/epub/docbook.xsl '{}' \;

When run as is, book1/book.xml is converted twice, with the output saving in the book1 directory, with the second transform saving over the first.

If I change -execdir to -exec:

find . -type f -name book.xml -exec xsltproc ../../../test/docbook-xsl-test/epub/docbook.xsl '{}' \;

then book1/book.xml is converted twice, with the output saving in the parent folder, then book2/book.xml is converted, with the output saving in the parent folder and saving over book1/book.xml conversion (the output is named the same).

Removing the -execdir altogether results in the following error:

find: paths must precede expression xsltproc

thanks

crts 01-06-2011 05:36 PM

When colucix said to run without the -execdir option he meant this:
find . -type f -name book.xml

anhedonia 01-06-2011 05:58 PM

oh, sorry. when I run

find . -type f -name book.xml

My results are:

./book1/book.xml
./book2/book.xml

crts 01-06-2011 06:29 PM

The output looks fine. With the -execdir option your script will be executed once from book1 folder and again from inside book2.
I noticed that you changed the calling parameters of your script. Maybe it has something to do with that? Or maybe the script itself performs some searching for files named book.xml? So, I think you will have to post your xmlscript for further analysis.


All times are GMT -5. The time now is 11:51 AM.