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”

  • Git push over SSH on Windows won't work
  • How to make “git branch” respect the “core.ignorecase” flag on Linux?
  • Git is Unusably Slow on Windows 10
  • Why can't Git Bash run my executable?
  • starting with Git on Windows, receiving sh.exe" : config: command not found
  • Git error in Windows 7: git did not exit cleanly (exit code 128)
  • SVN says I need to perform cleanup, but the cleanup fails
  • Powershell script to call git init in all subdirectories
  • 2 Solutions collect form web for “Git move & commit all directories matching a string with Bash”

    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_/}' \;

    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_/}'
    
    Git Baby is a git and github fan, let's start git clone.