home projects speaker

How to rename multiple files with git mv

I needed a solution where I had to rename multiple files with git mv, instead of renaming every file first, then use git rm and then git add. Came across a solution that I have tweaked to fit my need

bash
# Rename multiple files
# http://www.24hourapps.com/2009/03/linux-tips-10-rename-multiple-files.html
# https://gist.github.com/alobato/397988
#
# Found the orignal gist very helpfull, and tweaked it to fit git mv

# First I suggest you test the outcome before you do the renaming, with a dry run (-n)

for f in $(git ls-files | grep %filestomatch%); do git mv -n "${f}" "${f/%filestomatch%/%newfilename_orlocation%}"; done;

# If you are happy with the result, execute ( remove -n )

for f in $(git ls-files | grep %filestomatch%); do git mv "${f}" "${f/%filestomatch%/%newfilename_orlocation%}"; done;

# It is quite a long command, but it basically executes a loop and tells it to take each f file
# name in the grepped result from git ls-files and give it a name where a match for %filestomatch% is replaced with %newfilename_orlocation%.

About the author

Hi! My name is Alexander, and I am a creative frontender, specializing in UX, accessibility, universal design, frontend-architecture, node and design systems. I am passionate with open source projects and love to dabble with new emerging technologies related to frontend. With over 24 years of frontend experience, I have earned the right to be called a veteran. I am a lover of life, technologist at heart. If I am not coding, I am cooking and I love whisky and cigars. Oh, and coffee, I LOVE coffee!

If you want to know more about me, here is some links you might want to check out: GitHub, Instagram, Twitter, LinkedIn, CodePen, Slides.com, npm,

Speaker

I am also an avid speaker on several topics! Check out some of the things I speak about, and contact me if you are interested in having me at your next event!