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 |
what language are you using? I recall "foreach" existing in perl and php...
|
I am using linux. I assumed foreach based on some google searches. Not married to it.
|
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:
|
Quote:
Code:
echo $SHELL Code:
#!/bin/tcsh |
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 \; Or maybe even better yet: Code:
find /path/to/parent/ -type f -name xmlfile -exec /path/to/xmlscript '{}' \; |
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 |
The -execdir option of find does exactly what you're looking for:
Code:
-execdir command {} + |
-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 |
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 '{}' \; Code:
find /path/to/parent -type f -name 'book.xml' -execdir /path/to/xml/script '{}' \; |
Apparently I cannot see any reason for this to happen. What is the output of the find command without the -execdir option?
|
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 |
When colucix said to run without the -execdir option he meant this:
find . -type f -name book.xml |
oh, sorry. when I run
find . -type f -name book.xml My results are: ./book1/book.xml ./book2/book.xml |
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. |