Luca
Luca

Reputation: 595

Git mv Files According to Regex Substitution

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.

Upvotes: 1

Views: 217

Answers (1)

Dan Lowe
Dan Lowe

Reputation: 56657

This is easier with a for loop around it.

for f in *_1.txt; do
    echo git mv $f ${f/_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 inline substitution works like this:

${VAR/_1.txt/}

Where VAR is the variable name, and the pattern to remove is between the /s. Here it's simply text, but you can use regular expressions here.

Upvotes: 2

Related Questions