5

If you have a series of sub folders (like from a to z) and want to run a command on each one of them (like dsmmigrate * & ) how do you do that? The manual approach would be,

cd a 
dsmmigrate * &
cd ../b

That seems too complicated, so I believe there must be an easier approach.

1
  • 3
    find . -type d -print0 | xargs -0 -n1 echo do somthing Jul 8, 2016 at 5:15

3 Answers 3

5

Yes, changing the working directory back and forth is cumbersome and not really what you would like to do as it can lead to extremely weird situations in more complex scripts, unless you are careful.

The usual method for changing the working directory for a simple command is to put the cd and to invocation of the command in a sub-shell. The working directory will be changed for the sub-shell but the change is not carried over to the rest of the script as the sub-shell is executing in its own environment.

Example: Executing mycommand inside all directories in the current working directory:

for d in *; do
  if [ -d "$d" ]; then         # or:  if test -d "$d"; then
    ( cd "$d" && mycommand )
  fi
done

or in your case, with known directories a and b:

for d in a b; do
  ( cd "$d" && dsmmigrate * & )
done

I don't know the dsmmigrate tool, so I can't say whether running it this way is right or not.

EDIT: It turns out that the dsmmigrate tool has a -Recursive flag:

$ dsmmigrate -Recursive /path
17
  • Let say I can't place the script in the root directory but I want the command to apply to all the folders in the root directory, do I just add a cd "root directory"? In the shel code above ? Above the shell code ? Thanks
    – SuperKing
    Jul 26, 2016 at 22:45
  • 1
    @SuperKing No, it is likely that you can't place the script in the root directory, and you don't need to either. If you want to do this for all folders in the root directory (and you know that this is the right thing to do), then take the one of my example loops (the first one will run folder by folder) and change the frst line to for d in /*; do.
    – Kusalananda
    Jul 27, 2016 at 5:31
  • getting an error , syntax error at line 5; 'do for d in path/*; do if [ -d "$d" ]; then # or: if test -d "$d"; then ( cd "$d" && dsmmigrate * & ) fi done done
    – SuperKing
    Jul 27, 2016 at 15:37
  • 1
    @SuperKing Ok, so I don't know what the command does, but according to its manual, you should be able to run it as dsmmigrate -Recursive /path.
    – Kusalananda
    Jul 27, 2016 at 20:11
  • 1
    See also for d in */ to avoid the [ -d "$d" ]. Jul 27, 2016 at 20:34
3

You can do the following, when your current directory is parent_directory:

for d in [a-z]
do
    ( cd $d && your-command-here )
done

The ( and ) create a subshell, so the current directory isn't changed in the main script.

1
  • 1
    did any of answer helped you ? If yes, then please consider accepting answer by clicking on right mark below up/down arrow button.
    – Rahul
    Jul 11, 2016 at 10:28
2

This is my way of submitting several jobs in the subfolders :

for d in */; do ( cd "$d" && yourcommand ) ; done   

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .