I am trying to rename many files in my application and need to be able to do a rename in all subdirectories from the app root through git (i.e. git mv %filenamematch% %replacement%) that only replaces the matching text. I'm no good with bash scripting though.
update: would be good it if also renamed directories that match as well!
This should do the trick:
for file in $(git ls-files | grep %filenamematch% | sed -e 's/\(%filenamematch%[^/]*\).*/\1/' | uniq); do git mv $file $(echo $file | sed -e 's/%filenamematch%/%replacement%/'); done
To follow what this is doing, you'll need to understand piping with "|" and command substitution with "$(...)". These powerful shell constructs allow us to combine several commands to get the result we need. See Pipelines and Command Substitution.
Here's what's going on in this one-liner:
git ls-files
: This produces a list of files in the Git repository. It's similar to what you could get from ls
, except it only outputs Git project files. Starting from this list ensures that nothing in your .git/ directory gets touched.
| grep %filenamematch%
: We take the list from git ls-files
and pipe it through grep to filter it down to only the file names containing the word or pattern we're looking for.
| sed -e 's/\(%filenamematch%[^/]*\).*/\1/'
: We pipe these matches through sed (the stream editor), executing (-e) sed's s (substitute) command to chop off any / and subsequent characters after our matching directory (if it happens to be one).
| uniq
: In cases where the match is a directory, now that we've chopped off contained directories and files, there could be many matching lines. We use uniq to make them all into one line.
for file in ...
: The shell's "for" command will iterate through all the items (file names) in the list. Each filename in turn, it assigns to the variable "$file" and then executes the command after the semicolon (;).
sed -e 's/%filenamematch%/%replacement%/'
: We use echo to pipe each filename through sed, using it's substitute command again--this time to perform our pattern replacement on the filename.
git mv
: We use this git command to mv the existing file ($file) to the new filename (the one altered by sed).
One way to understand this better would be to observe each of these steps in isolation. To do that, run the commands below in your shell, and observe the output. All of these are non-destructive, only producing lists for your observation:
git ls-files
git ls-files | grep %filenamematch%
git ls-files | grep %filenamematch% | sed -e 's/\(%filenamematch%[^/]*\).*/\1/'
git ls-files | grep %filenamematch% | sed -e 's/\(%filenamematch%[^/]*\).*/\1/' | uniq
for file in $(git ls-files | grep %filenamematch% | sed -e 's/\(%filenamematch%[^/]*\).*/\1/' | uniq); do echo $file
; done
for file in $(git ls-files | grep %filenamematch% | sed -e 's/\(%filenamematch%[^/]*\).*/\1/' | uniq); do echo $file | sed -e 's/%filenamematch%/%replacement%/'
; done
Rename the files with a regular expression using the command rename
:
rename 's/old/new/' *
Then register the changes with Git by adding the new files and deleting the old ones:
git add .
git ls-files -z --deleted | xargs -0 git rm
In newer versions of Git you can use the --all
flag instead:
git add --all .
Late to the party but, this should work in BASH (for files and directories, but I'd be careful regarding directories):
find . -name '*foo*' -exec bash -c 'file={}; git mv $file ${file/foo/bar}' \;
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With