markwalker_
markwalker_

Reputation: 12869

Git move & commit all directories matching a string with Bash

I've got about 150 directories which I want to rename (and commit) in a git repo. The paths are something similar to;

/src/app/testing/linux/testindexscreen_home/image.png
/src/app/testing/osx/testindexscreen_home/image.png
/src/app/testing/win/testindexscreen_home/image.png

So I'd like to run mv and then commit on all paths which match indexscreen_ to remove that part of the string.

I'm on a windows box, using git bash and at the moment have the find & mv command trying to move the folder in to itself. I'm not sure how you remove the matched string;

find . -name '*indexscreen_*' -exec sh -c 'file={}; git mv $file ${file/"*indexscreen_*"/}' \;

Which with the commit included I think needs to be;

find . -name '*indexscreen_*' -exec sh -c 'file={}; git mv $file ${file/"*indexscreen_*"/}; git commit -m "Renamed $file"' \;

So I'd like to have that bash command turn those paths in to;

/src/app/testing/linux/testhome/image.png
/src/app/testing/osx/testhome/image.png
/src/app/testing/win/testhome/image.png

And have commit messages like "Renamed testhome"

Upvotes: 1

Views: 139

Answers (2)

slobobaby
slobobaby

Reputation: 706

I think this does what you want:

find . -name "*indexscreen_*" -a -type d -exec sh -c 'n={}; git mv {} ${n/indexscreen_/}' \;

but my find complains when I disappear the files while find is running.

The same solution using xargs does not complain:

find . -name "*indexscreen_*" -a -type d |xargs -i sh -c 'n={}; git mv {} ${n/indexscreen_/}'

Upvotes: 0

markwalker_
markwalker_

Reputation: 12869

So my understanding of bash hasn't improved much here (I need to find some good docs), but I have figured out the problem with what I was running.

Changing my mv command to the following successfully renamed the directories as I wanted;

find . -name '*indexscreen_*' -exec sh -c 'file={}; git mv $file ${file/indexscreen_/}' \;

Upvotes: 1

Related Questions