2

I have a directory with many subdirectories inside, i want to execute a command on each of those subdirectories.

What i want to do is run 'svn up'

this is what i have tried so far

find . -type d -maxdepth 1 -exec svn "up '{}'" \;

and

for dir in * do cd $dir; svn up; cd ..;

None of them works so far (I have tried many things without luck)

3 Answers 3

9

You just need a trailing slash on the glob:

for d in */; do # only match directories
  ( cd "$d" && svn up ) # Use a subshell to avoid having to cd back to the root each time.
done
Sign up to request clarification or add additional context in comments.

1 Comment

@ArnoldRoa heh, but the trailing slash is the real answer here. :P
3

This seems to work:

find . -type d -maxdepth 1 -exec svn up "{}" \;

But it tried to update the current directory, which should be ommited. (althought it works for me because current dir is not a svn directory)

Comments

3

This works for me - the -d checks for a directory:

for f in *; do if [ -d "$f" ]; then cd "$f"; echo "$f"; cd ..; fi; done

echo "$f" can be substituted for whatever command you wish to run from inside each directory.

Note that this, and the trailing / solution, both match symbolic links, as well as files. If you want to avoid this behaviour (only enter real directories), you can do this:

for f in *; do if [ -d "$f" -a ! -L "$f" ]; then cd "$f"; echo "$f"; cd ..; fi done

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.