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.

  • 2
    find . -type d -print0 | xargs -0 -n1 echo do somthing – ctrl-alt-delor Jul 8 '16 at 5:15

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 )

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

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

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
  • 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 '16 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 '16 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 '16 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 '16 at 20:11
  • 1
    See also for d in */ to avoid the [ -d "$d" ]. – Stéphane Chazelas Jul 27 '16 at 20:34

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

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

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

  • 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 '16 at 10:28

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.