使用 Bash 重命名名称中间的多个文件的一小部分?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11053558/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Rename small part of multiple files in middle of name using Bash?
提问by cc211
I'd just like to change this
我只想改变这个
cc211_AMBER_13062012i.II cc211_GROMOS_13062012i.II
cc211_CHARM_13062012i.II cc211_OPLS_13062012i.II
to
到
cc211_AMBER_15062012i.II cc211_GROMOS_15062012i.II
cc211_CHARM_15062012i.II cc211_OPLS_15062012i.II
I tried,
我试过,
find -name "*.13 *" | xargs rename ".13" ".15"
There is normally no space between the 3 and the second asterix, thats just makes it italics on from what I can see. Basically there's a lot of answers for what to do when it's at the end of the filename, where asterix seem to work, but here I can't make it work.
3 和第二个星号之间通常没有空格,这只是我所看到的斜体。基本上,当它位于文件名末尾时(星号似乎可以工作),有很多答案可以解决,但在这里我无法使其工作。
Anything you've got would make my life a lot easier!
你拥有的任何东西都会让我的生活更轻松!
Edit 1: Trial
编辑 1:审判
-bash-4.1$ ls
cc211_AMBER_13062012.II cc211_GROMOS_13062012.II
cc211_CHARM_13062012.II cc211_OPLS_13062012.II
-bash-4.1$ rename 's/_13/_15/' cc*
-bash-4.1$ ls
cc211_AMBER_13062012.II cc211_GROMOS_13062012.II
cc211_CHARM_13062012.II cc211_OPLS_13062012.II
回答by Anthony
回答by chepner
A pure bashsolution:
一个纯粹的bash解决方案:
for i in cc*; do
mv "$i" "${i/_13/_15}"
done
回答by John Lawrence
rename 's/_13/_15/' cc*
Should do what you want. The regular expression s/_13/_15/replaces _13by _15in all files starting 'cc'.
应该做你想做的。正则表达式在所有以“cc”开头的文件中s/_13/_15/替换_13为_15。
$ ls
cc211_AMBER_13062012.II cc211_GROMOS_13062012.II
cc211_CHARM_13062012.II cc211_OPLS_13062012.II
$ rename 's/_13/_15/' cc*
$ ls
cc211_AMBER_15062012.II cc211_GROMOS_15062012.II
cc211_CHARM_15062012.II cc211_OPLS_15062012.II
This will only work with the newer perl version of rename. To check which version you have do man rename. If the top of the page says
这仅适用于较新的 perl 版本的rename. 要检查您使用的是哪个版本man rename。如果页面顶部说
Perl Programmers Reference Guide
Perl 程序员参考指南
you have the perl version. If it says:
你有 perl 版本。如果它说:
Linux Programmer's Manual
Linux 程序员手册
you have the standard (older) version.
你有标准(旧)版本。
For the older version, the command should be:
对于旧版本,命令应该是:
rename _13 _15 cc*
回答by jesramgue
I'm using a pure Linux solution:
我使用的是纯 Linux 解决方案:
### find all files that contains _DES in name and duplicate them adding _AUXLOCAL
for f in **/*_DES*; do
cp "$f" "${f%.DES}_AUXLOCAL"
done
###Rename all _AUXLOCAL files, removing _DES to _LOCAL
for f in **/*_AUXLOCAL*; do
mv "$f" "${f/_DES/_LOCAL}"
done
###Rename all _AUXLOCAL files, removing _AUXLOCAL
for f in **/*_AUXLOCAL*; do
mv "$f" "${f/_AUXLOCAL/}"
done
I hope it helps
我希望它有帮助

