Question

I have this little script that puts out some dir paths. I want to mkdir all those paths.

for filename in $(git ls-files | grep .java | grep -v 'com/foo' | sed -e 's/\(.java[^/]*\).java/\1/' | uniq)
do
echo "$filename" | sed -e 's/com\/old\/old/com\/new/' | sed 's/\(.*\)\/.*/\1/'
done

So new dir are created... com/old/old = com/new

But I cannot get that mkdir to work... I tried..

path=$("$filename" | sed -e 's/com\/old\/old/com\/new/' | sed 's/\(.*\)\/.*/\1/')
mkdir -p "$path"

That is just messing with the file contents.

Was it helpful?

Solution

You should be able to do all the substitutions in one sed command, and it can also filter out com/foo. Then pipe the output to a while read loop.

git ls-files | grep .java |
        sed -e '/com\/foo/d' -e 's/\(.java[^/]*\).java/\1/' -e 's/com\/old\/old/com\/new/' -e 's/\(.*\)\/.*/\1/' |
        uniq | while read path; do
    mkdir -p "$path"
done

Here's how to do your git mv:

git ls-files | grep .java |
        sed -e '/com\/intuit/d' -e 's/\(.java[^/]*\).java/\1/' | uniq | 
    while read path; do
        dir=$(echo "$path" | sed -e 's/com\/old\/old/com\/new/' -e 's/\(.*\)\/.*/\1/')
        mkdir -p "$dir"
        git mv "$path" "$dir"
done
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top