LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
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 08-01-2014, 02:34 PM   #1
mamunm
LQ Newbie
 
Registered: Apr 2014
Posts: 12

Rep: Reputation: Disabled
for loop to run a script in all directories


I have folders like
VAS-M08*/*/*
I have to run ~/MKM/mkm in each folders.
I am using a for loop to do this but the problem is it executes the script in even subfolders in some folders and odd subfolders in some folders. My for loop is
for subdir in V*/*/ ; do cd "$subdir"; ~/MKM/mkm; cd ..; done

Can anyone help me out here. Your help will be highly appreciated.
Thanks,
Mamun
 
Old 08-01-2014, 02:48 PM   #2
suicidaleggroll
LQ Guru
 
Registered: Nov 2010
Location: Colorado
Distribution: OpenSUSE, CentOS
Posts: 5,396

Rep: Reputation: 2017Reputation: 2017Reputation: 2017Reputation: 2017Reputation: 2017Reputation: 2017Reputation: 2017Reputation: 2017Reputation: 2017Reputation: 2017Reputation: 2017
If you want to run something in all directories and all subdirectories of directories and so on, then you should either use a recursive function or something like "find . -type d".

The reason your current loop is failing is because you're cd'ing two subdirectories deep but only jumping back up one directory at the end, which doesn't bring you back to your starting location. The end result is the first cd works, the cd .. brings you back up one of the two directories you went down, the second cd fails because that directory doesn't exist from your current location, the cd .. brings you back to your original location so the third cd works, and so on.

You should be using "cd -" or "cd ../..", but even then you're only going to be looping through all directories that are 2 levels down from your cwd, you won't be looping through the directories that are 1 level down, or 3 levels down, or 4 levels down, etc. Whether or not this is a problem depends on what you're trying to do.

A better solution would be to modify the ~/MKM/mkm script so that it accepts the directory in which it should run as a command line argument, and make sure you can pass in multiples. Then you would simply call:
Code:
~/MKM/mkm V*/*/

Last edited by suicidaleggroll; 08-01-2014 at 02:51 PM.
 
Old 08-02-2014, 12:52 AM   #3
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,437

Rep: Reputation: 2842Reputation: 2842Reputation: 2842Reputation: 2842Reputation: 2842Reputation: 2842Reputation: 2842Reputation: 2842Reputation: 2842Reputation: 2842Reputation: 2842
I agree with the previous post, however, I believe you have a bigger issue.

You stated:
Quote:
I have folders like
VAS-M08*/*/*
If you really have folders / directories named this way you are going to have an extremely difficult time navigating as you will need to escape the asterisks and the forward slashes
so your script knows this is the actual name of a folder / directory
 
Old 08-02-2014, 05:00 AM   #4
jpollard
Senior Member
 
Registered: Dec 2012
Location: Washington DC area
Distribution: Fedora, CentOS, Slackware
Posts: 4,661

Rep: Reputation: 1256Reputation: 1256Reputation: 1256Reputation: 1256Reputation: 1256Reputation: 1256Reputation: 1256Reputation: 1256Reputation: 1256
Quote:
Originally Posted by mamunm View Post
I have folders like
VAS-M08*/*/*
I have to run ~/MKM/mkm in each folders.
I am using a for loop to do this but the problem is it executes the script in even subfolders in some folders and odd subfolders in some folders. My for loop is
for subdir in V*/*/ ; do cd "$subdir"; ~/MKM/mkm; cd ..; done

Can anyone help me out here. Your help will be highly appreciated.
Thanks,
Mamun
Like others are saying, you are changing to multiple depths then backing up only one.

The simple soluction is:
Code:
for subdir in V*/*/ ; do
    (cd "$subdir"; ~/MKM/mkm )
done
The parentheses cause the shell to fork a new process to run the two commands. When the mkm script finishes, so does the process, and the current process (before the cd "$subdir") is still set.

This may be a bit slower, and certainly creates extra processes (at least one for every directory path) so using a find is faster (partly due to the fact that find forks the process to run the two commands), but also will avoid non-directory files.

Alternatively you could do:
Code:
BASE=`pwd`
for subdir in V*/*/ ; do
    cd "$subdir"; ~/MKM/mkm; cd "$BASE"
done
As this determines what the base directory is and always goes back to it.
 
1 members found this post helpful.
  


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
[SOLVED] How to run csh script on files in different directories TalkingRice Linux - Newbie 12 07-08-2014 12:42 PM
[SOLVED] Need for loop to get dirs and run bash script bckr Linux - Newbie 5 07-07-2010 10:42 PM
Shell Script: run multi for-loop at the same time? Dr_Death_UAE Programming 2 11-10-2009 10:09 AM
Run script in a loop anjanesh Linux - General 5 03-07-2007 10:16 PM
Script to run script in multiple directories... seabass55 Linux - General 5 07-06-2004 12:44 AM


All times are GMT -5. The time now is 04:32 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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration