git mv文件根据正则替换

发布于 2025-01-29 09:07:38 字数 269 浏览 2 评论 0原文

我正在尝试重命名大量错误命名的文件,这些文件已在我的本地GIT存储库中跟踪。这些文件当前是foo.txt_1.txt的形式。我想将它们更改为foo.txt

这就是我到目前为止一直在尝试的事情:

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

如果newfileName我想要替换,我不确定应该使用什么。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

谈情不如逗狗 2025-02-05 09:07:38

围绕它的循环更容易。

for f in *_1.txt; do
    echo git mv $f ${f/_1.txt/}
done

这将呼应它将运行的命令,以便您可以检查其正确。从“ git”命令之前从“ git”命令中删除“ echo”以实际执行。

bash inline替换工作类似:

${VAR/_1.txt/}

其中var是变量名称,而要删除的模式位于/ s之间。这只是文本,但是您可以在此处使用正则表达式。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文