How do I move or rename multiple files at once with git?

Question: Question:

I want to rename all erb files in a git repository to haml. (Example: index.html.erb => index.html.haml)

It is difficult to rename one by one with the following command.

$ git mv app/views/pages/index.html.erb app/views/pages/index.html.haml

Also, I thought I could use such a command, but I couldn't.

$ git mv app/views/**/*.erb app/views/**/*.haml

usage: git mv [options] <source>... <destination>

    -n, --dry-run         dry run
    -f, --force           force move/rename even if target exists
    -k                    skip move/rename errors

What should I do in such a case?

Answer: Answer:

If you enter the answer of the English version site like the questioner

for i in $(find . -iname "*.erb"); do
    git mv "$i" "$(echo $i | rev | cut -d '.' -f 2- | rev).haml";

However, this alone does not add value, so I will explain it.

$(find . -iname "*.erb")

Now list all the files ending in .erb . Let's look at it one by one with for .


$(echo $i | rev | cut -d '.' -f 2- | rev)

I will look at.

echo $i | rev

Reverses the path to the file with (eg app/views/person/index.html.erb > bre.xedni/nosrep/sweiv/ppa ).

cut -d '.' -f 2-

Then, separate the files with dots . And extract only the second and subsequent files (that is, delete only bre. ).

Finally, rev returns the character to its original positive direction. (Example: xedni/nosrep/sweiv/ppa > app/views/person/index )

with this

git mv "$i" "$(echo $i | rev | cut -d '.' -f 2- | rev).haml";

.erb from the original file name, adds .haml , and passes it to git mv .

Scroll to Top