How to Run a command on all subfolders
Solution 1
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
Solution 2
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.
Solution 3
This is my way of submitting several jobs in the subfolders :
for d in */; do ( cd "$d" && yourcommand ) ; done
Related videos on Youtube
basickarl
Updated on September 18, 2022Comments
-
basickarl almost 2 years
I am using this library: http://harthur.github.io/clusterfck/
I get the following extract from hierarchical clustering:
[ {"canonical":[20,120,102], "size":1 }, {"canonical":[250,255,253], "left":{ "canonical":[250,255,253], "size":1 }, "right":{ "canonical":[255,255,240], "size":1 }, "size":2 }, {"canonical":[100,54,300], "size":1 } ]
As you can see it does not state the ultrametric distance. Is there a way of getting the ultrametric distance data from the clustering? Or if there are any other libraries which can supply that value?
-
Has QUIT--Anony-Mousse about 9 yearsUse the source of clusterfck.
-
ctrl-alt-delor almost 8 years
find . -type d -print0 | xargs -0 -n1 echo do somthing
-
-
Rahul almost 8 yearsdid any of answer helped you ? If yes, then please consider accepting answer by clicking on right mark below up/down arrow button.
-
SuperKing almost 8 yearsLet 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
-
Kusalananda almost 8 years@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
. -
SuperKing almost 8 yearsgetting 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
-
Kusalananda almost 8 years@SuperKing The
do
at the start shouldn't be there. Look at what I wrote. And there's an extradone
at the end... -
SuperKing almost 8 yearsfor d in path/*; do if [ -d "$d" ]; then ( cd "$d" dsmmigrate * & ) fi done done
-
SuperKing almost 8 years' unexpectedh: syntax error at line 2 : `
-
Kusalananda almost 8 years@SuperKing Why do you still have two
done
at the end? If you remove that and make sure there are newlines afterdo
,then
,)
, andfi
, it ought to be correct. I just checked it with ShellCheck. -
SuperKing almost 8 yearsI only had one done, not sure why its showing 2 when I pasted. I think the issue is the format window format to unix ASCII
-
SuperKing almost 8 yearsWorks beautifully, however new small issue. in path/* only apply the command in the sub folders but it does not apply command to the folders within the subfolders
-
Kusalananda almost 8 years@SuperKing That wasn't part of the original question, but I'll add it to the answer in a few minutes.
-
SuperKing almost 8 yearsNot really sure how to do that, I'm a complete unix noob
-
Kusalananda almost 8 years@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
. -
Stéphane Chazelas almost 8 yearsSee also
for d in */
to avoid the[ -d "$d" ]
. -
SuperKing almost 8 years@Stéphane Chazelas , what do you mean?
-
Stéphane Chazelas almost 8 years@SuperKing, I mean that
for d in */
will loop on directories (and symlinks to directories) only, so then you don't need to test[ -d "$d" ]
.