18

I want to rename all erb files in my Git project to haml.(like index.html.erb to index.html.haml)

If I rename each file, I have to type the following command more than thirty times.

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

I tried the command below, but it did not work.

$ 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

How can I rename them at once?

Junichi Ito
  • 2,438
  • 1
  • 23
  • 46
  • It's the same as ... and other `mv` command. Move the directories one at a time, or write a quich bash loop to do what you're tyring to do right now if you don't want to move all the files. – Brian Roach Feb 05 '12 at 18:46

6 Answers6

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

For each .erb file, git mv it to itself with the extension ".erb" replaced by ".haml".

Borealid
  • 95,191
  • 9
  • 106
  • 122
  • 1
    You need to wrap second argument to `mv` in `"` or it won't work with file names with spaces. – Piotr Praszmo Feb 05 '12 at 18:54
  • @Banthar You only noticed because I got it right on my first answer :-P. Anyway, this isn't going to work with filenames containing the quotation mark - the "right" way is to run the names through `printf "%q"` or the like. – Borealid Feb 05 '12 at 18:56
  • In fact, it doesn't matter, because `for` will split on spaces anyway. I guess the best solution is to not put silly characters in your filenames. – Piotr Praszmo Feb 05 '12 at 19:10
  • My git bash in windows does not ship with the rev utility. – Eli S May 12 '22 at 04:36
9

A one liner using powershell

Dir *.erb -Recurse | foreach { git mv $_.FullName $_.FullName.replace("erb", "haml")}
chris31389
  • 8,414
  • 7
  • 55
  • 66
  • 2
    This works but will also replace all `erb` instances in the whole path, not only extension. What I used instead is this: `Dir *.h -Recurse | foreach { git mv $_.FullName ($_.Directory.FullName + "\" + $_.BaseName + ".hpp") }`. – Vennor Sep 19 '20 at 10:51
  • Only @Vennor suggestion worked for me – Ilya Gazman Feb 19 '22 at 21:45
  • This did not work for me exactly. I got a bunch of "fatal **** is outside repository" complaints, probably because of the Directory.FullName part. `Dir *.rdb | foreach { git mv $_.Name ($_.BaseName + ".csv") }` did the trick. – Eli S May 12 '22 at 04:53
3

This worked for me using Powershell:

Get-ChildItem -File | foreach { git mv ($_.BaseName+".js") ($_.BaseName+".ts") }

For safety I chose not to add -Recurse.

Replace file extensions with whatever you need.

Victor Ian
  • 1,034
  • 13
  • 26
  • 1
    Very handy, thanks. In my instance, I wanted to move en masse and replace spaces, so amended your code to `Get-ChildItem -File | foreach { git mv ($_.name) ($_.name –replace " ","_") }` – Pauk Apr 23 '19 at 14:40
  • @Pauk Learned something new with that. Thanks! – Victor Ian Apr 24 '19 at 01:23
3

Further to @Borealid's answer, it can be simpler:

for i in $(find . -iname "*.[old-extension]"); do
  git mv "$i" "${i/[old-extension]/[new-extension]}";
done

While [old-extension] and [new-extension] should replaced with real extension.

The slash symbols (/) is the bash syntax of replace in strings.

Mosh Feu
  • 28,354
  • 16
  • 88
  • 135
0

In windows, you need 2 batch files. 1 we will generate, the other is fixed.

Example below to git rename recursively all *.js into *.ts Assumption: filename has no empty spaces.

Step 1: fixed file git-rn.bat

@echo off
set _srcfile=%1
set _tgtfile=%_srcfile:.js=.ts%
git mv %_srcfile% %_tgtfile%

Save it at the same place/directory where git-rn-all.bat

Step 2: generate file git-rn-all.bat by using a command

cd 'C:\projects\some-git-project\src\client\app' dir /a /s /b *.js > git-rn-all.bat

Step 3: Modify git-rn-all.bat for our needs.

Edit git-rn-all.bat and text-replace-all the common base path to add a call to the git-rn.bat. Any text editor can easily do that.

example:

C:\projects\some-git-project\src\client\app\app.module.js

becomes

call git-rn.bat C:\projects\some-git-project\src\client\app\app.module.js

Step 4: execute git-rn-all.bat

you will see quite a big output

Step 5: remove batch files

remove git-rn-all.bat and git-rn.bat you probably don't need them any longer.

Igor Lino
  • 532
  • 7
  • 10
0

Although it's not the answer to the exact question here, it still matches this question title, so I think it may serve others like me, who got here googling to perform git rename by adding suffix before file extension (it worked for me):

for i in *.<file extension>
do
    git mv -v "${i}" "${i%.*}_SUFFIX.${i##*.}"
done
  • <file extension> should be replaced with your required type, E.g. c for C files
  • _SUFFIX should be replaced with your required suffix before file type
  • In case you don't use git and just Linux bash, simply remove git from the command
GM1
  • 380
  • 3
  • 14