[SOLVED] Using foreach to run same command in several folders
Linux - NewbieThis 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
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
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?
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...
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?
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.
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 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.
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:
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.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.