tags:

views:

60

answers:

2

I want to rename all nested directories named "foo" to "bar" - I've tried the following with no joy:

find */ -name 'foo' | xargs svn move {} 'bar' \;

Thanks

+1  A: 

That will attempt to move each foo to pwd/bar and passes svn move too many arguments. Here's what I would do:

find . -depth -type d -name 'foo' -print | while read ; do echo svn mv $REPLY `dirname $REPLY`/bar ; done 

You can remove the echo to have it actually perform the operation. The above works under the assumption that you don't have spaces in your filenames.

Kaleb Pederson
You might want to `sort --reverse` the `find` results before the `while read`. To avoid renaming a nested directory **after** renaming its' parent... Also have a `-type d` in the find for good measure.
Chen Levy
@Chen - good suggestions. Added `-depth` to handle the nested case and `-type d` "for good measure."
Kaleb Pederson
A: 

You could use bash to visit manually the directory tree using a post-order walk:

#!/bin/bash

visit() {
local file
for file in $1/*; do 
    if [ -d "$file" ]; then
        visit "$file";
        if [[ $file =~ /foo$ ]]; then
            svn move $file ${file%foo}bar;
        fi                  
    fi
done
}

if [ $# -ne 1 ]; then
exit
fi

visit $1

This code doesn't have any infinite loops detection, but should work in simple cases.

marco