重命名多个文件,但在 Bash 中只重命名部分文件名
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/20657432/
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 multiple files, but only rename part of the filename in Bash
提问by user3115029
I know how I can rename files and such, but I'm having trouble with this.
我知道如何重命名文件等,但我遇到了麻烦。
I only need to rename test-this
in a for loop.
我只需要test-this
在 for 循环中重命名。
test-this.ext
test-this.volume001+02.ext
test-this.volume002+04.ext
test-this.volume003+08.ext
test-this.volume004+16.ext
test-this.volume005+32.ext
test-this.volume006+64.ext
test-this.volume007+78.ext
回答by Tim Zimmermann
If you have all of these files in one folder and you're on Linux you can use:
如果您在一个文件夹中拥有所有这些文件并且您使用的是 Linux,则可以使用:
rename 's/test-this/REPLACESTRING/g' *
The result will be:
结果将是:
REPLACESTRING.ext
REPLACESTRING.volume001+02.ext
REPLACESTRING.volume002+04.ext
...
rename
can take a command as the first argument. The command here consists of four parts:
rename
可以将命令作为第一个参数。这里的命令由四部分组成:
s
: flag to substitute a string with another string,test-this
: the string you want to replace,REPLACESTRING
: the string you want to replace the search string with, andg
: a flag indicating that all matches of the search string shall be replaced, i.e. if the filename istest-this-abc-test-this.ext
the result will beREPLACESTRING-abc-REPLACESTRING.ext
.
s
: 用另一个字符串替换一个字符串的标志,test-this
:要替换的字符串,REPLACESTRING
:要替换搜索字符串的字符串,以及g
: 一个标志,指示搜索字符串的所有匹配项都将被替换,即如果文件名是,test-this-abc-test-this.ext
则结果将为REPLACESTRING-abc-REPLACESTRING.ext
.
Refer to man sed
for a detailed description of the flags.
man sed
有关标志的详细说明,请参阅参考资料。
回答by dogbane
Use rename
as shown below:
使用rename
如下图:
rename test-this foo test-this*
This will replace test-this
with foo
in the file names.
这将替换文件名中的test-this
with foo
。
If you don't have rename
use a for
loop as shown below:
如果您没有rename
使用for
循环,如下所示:
for i in test-this*
do
mv "$i" "${i/test-this/foo}"
done
回答by Joe Flack
Function
功能
I'm on OSX and my bash doesn't come with rename
as a built-in function. I create a function in my .bash_profile
that takes the first argument, which is a pattern in the file that should only match once, and doesn't care what comes after it, and replaces with the text of argument 2.
我在 OSX 上,我的 bash 没有rename
作为内置函数提供。我在 my.bash_profile
中创建了一个函数,它接受第一个参数,它是文件中的一个模式,应该只匹配一次,并且不关心后面的内容,并替换为参数 2 的文本。
rename() {
for i in *
do
mv "$i" "${i//}"
done
}
Input Files
输入文件
test-this.ext
test-this.volume001+02.ext
test-this.volume002+04.ext
test-this.volume003+08.ext
test-this.volume004+16.ext
test-this.volume005+32.ext
test-this.volume006+64.ext
test-this.volume007+78.ext
Command
命令
rename test-this hello-there
rename test-this hello-there
Output
输出
hello-there.ext
hello-there.volume001+02.ext
hello-there.volume002+04.ext
hello-there.volume003+08.ext
hello-there.volume004+16.ext
hello-there.volume005+32.ext
hello-there.volume006+64.ext
hello-there.volume007+78.ext