Home > database >  Git mv Files According to Regex Substitution
Git mv Files According to Regex Substitution

Time:05-17

I am trying to rename a large number of incorrectly named files, which are tracked in my local git repo. The files are currently of the form foo.txt_1.txt. And I want to change them to foo.txt.

This is what I have been experimenting with so far:

ls *_1.txt | xargs -I {} git mv -n {} newfilename

I'm just not sure what I should use in place if newfilename to make the substitution I want.

CodePudding user response:

This is easier with a for loop.

for f in *_1.txt
do
    echo git mv $f $(echo $f | sed 's/_1.txt//')
done

This will echo the commands it would run so you can check it's correct. Remove "echo" from before the "git" command to actually execute.

Bash can do substitution inline, e.g.

${VAR/_1.txt/}

But in this case there's no variable, so you can't use a substitution.

  • Related