Renaming multiple files in git

Git has a command that lets you move files, which is also used to rename files. If you want to rename multiple files, you have to do each file individually.

There is some relief, however. If you’re on Linux/Unix and using bash, or if you’re on Windows using git bash, you can rename multiple tracked files, using the find command, with the -exec option.

find . -name '*.txt' -exec sh -c 'git mv "$0" "${0%.txt}.md"' {} \;

The command shown above will rename all .txt files to .md.

find . -name '*_-_*.md' -exec sh -c 'git mv "$0" "${0//_-_/_}"' {} \;

This variant will replace _-_ in a filename with _.

Add the -n flag to the git mv command to do a dry-run. A dry-run will show you what is going to happen. I found this useful when the substitution looked like it was going to rename folders as well; I got round this by running the command in the sub-folder.

References

The following links helped me figure this out, so it’s only right to mention them.

Changing file extensions in bulk using Git Bash

https://stackoverflow.com/questions/1224766/how-do-i-rename-the-extension-for-a-bunch-of-files/27285610#27285610

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s