LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
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 01-06-2011, 01:31 PM   #1
anhedonia
LQ Newbie
 
Registered: Jan 2011
Posts: 8

Rep: Reputation: 0
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
 
Old 01-06-2011, 02:00 PM   #2
acid_kewpie
Moderator
 
Registered: Jun 2001
Location: UK
Distribution: Gentoo, RHEL, Fedora, Centos
Posts: 43,417

Rep: Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985
what language are you using? I recall "foreach" existing in perl and php...
 
Old 01-06-2011, 02:06 PM   #3
anhedonia
LQ Newbie
 
Registered: Jan 2011
Posts: 8

Original Poster
Rep: Reputation: 0
I am using linux. I assumed foreach based on some google searches. Not married to it.
 
Old 01-06-2011, 02:08 PM   #4
acid_kewpie
Moderator
 
Registered: Jun 2001
Location: UK
Distribution: Gentoo, RHEL, Fedora, Centos
Posts: 43,417

Rep: Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985
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...
 
Old 01-06-2011, 02:12 PM   #5
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983
Quote:
Originally Posted by anhedonia View Post
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.
 
1 members found this post helpful.
Old 01-06-2011, 02:27 PM   #6
crts
Senior Member
 
Registered: Jan 2010
Posts: 2,020

Rep: Reputation: 757Reputation: 757Reputation: 757Reputation: 757Reputation: 757Reputation: 757Reputation: 757
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 '{}' \;
 
1 members found this post helpful.
Old 01-06-2011, 03:41 PM   #7
anhedonia
LQ Newbie
 
Registered: Jan 2011
Posts: 8

Original Poster
Rep: Reputation: 0
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
 
Old 01-06-2011, 03:45 PM   #8
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983
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.
 
1 members found this post helpful.
Old 01-06-2011, 04:50 PM   #9
anhedonia
LQ Newbie
 
Registered: Jan 2011
Posts: 8

Original Poster
Rep: Reputation: 0
-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
 
Old 01-06-2011, 05:10 PM   #10
crts
Senior Member
 
Registered: Jan 2010
Posts: 2,020

Rep: Reputation: 757Reputation: 757Reputation: 757Reputation: 757Reputation: 757Reputation: 757Reputation: 757
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.

Last edited by crts; 01-06-2011 at 05:11 PM.
 
1 members found this post helpful.
Old 01-06-2011, 05:17 PM   #11
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983
Apparently I cannot see any reason for this to happen. What is the output of the find command without the -execdir option?
 
1 members found this post helpful.
Old 01-06-2011, 05:31 PM   #12
anhedonia
LQ Newbie
 
Registered: Jan 2011
Posts: 8

Original Poster
Rep: Reputation: 0
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
 
Old 01-06-2011, 05:36 PM   #13
crts
Senior Member
 
Registered: Jan 2010
Posts: 2,020

Rep: Reputation: 757Reputation: 757Reputation: 757Reputation: 757Reputation: 757Reputation: 757Reputation: 757
When colucix said to run without the -execdir option he meant this:
find . -type f -name book.xml
 
1 members found this post helpful.
Old 01-06-2011, 05:58 PM   #14
anhedonia
LQ Newbie
 
Registered: Jan 2011
Posts: 8

Original Poster
Rep: Reputation: 0
oh, sorry. when I run

find . -type f -name book.xml

My results are:

./book1/book.xml
./book2/book.xml
 
Old 01-06-2011, 06:29 PM   #15
crts
Senior Member
 
Registered: Jan 2010
Posts: 2,020

Rep: Reputation: 757Reputation: 757Reputation: 757Reputation: 757Reputation: 757Reputation: 757Reputation: 757
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.
 
  


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
find -exec command to recursively delete files and folders in folders with X name Joan Murt Linux - Newbie 2 07-08-2009 04:35 PM
how to run tar wtihout re-create folders canadatom Linux - Newbie 3 06-26-2009 01:47 PM
How to enter graphical mode (run level 5) command line (run lenel 3) edmondgyampoh Linux - Newbie 3 05-15-2009 06:33 PM
command to delete folders on /tmp ceantuco Linux - Newbie 7 01-08-2009 08:10 AM
Is there a command that gives you ownership of a folder and all it's sub-folders royeo Linux - Newbie 3 06-01-2007 01:55 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 05:40 AM.

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
Open Source Consulting | Domain Registration