重命名多个文件,但在 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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-10 00:33:07  来源:igfitidea点击:

Rename multiple files, but only rename part of the filename in Bash

bashfile-rename

提问by user3115029

I know how I can rename files and such, but I'm having trouble with this.

我知道如何重命名文件等,但我遇到了麻烦。

I only need to rename test-thisin 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
...

renamecan take a command as the first argument. The command here consists of four parts:

rename可以将命令作为第一个参数。这里的命令由四部分组成:

  1. s: flag to substitute a string with another string,
  2. test-this: the string you want to replace,
  3. REPLACESTRING: the string you want to replace the search string with, and
  4. g: a flag indicating that all matches of the search string shall be replaced, i.e. if the filename is test-this-abc-test-this.extthe result will be REPLACESTRING-abc-REPLACESTRING.ext.
  1. s: 用另一个字符串替换一个字符串的标志,
  2. test-this:要替换的字符串,
  3. REPLACESTRING:要替换搜索字符串的字符串,以及
  4. g: 一个标志,指示搜索字符串的所有匹配项都将被替换,即如果文件名是,test-this-abc-test-this.ext则结果将为REPLACESTRING-abc-REPLACESTRING.ext.

Refer to man sedfor a detailed description of the flags.

man sed有关标志的详细说明,请参阅参考资料。

回答by dogbane

Use renameas shown below:

使用rename如下图:

rename test-this foo test-this*

This will replace test-thiswith fooin the file names.

这将替换文件名中的test-thiswith foo

If you don't have renameuse a forloop 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 renameas a built-in function. I create a function in my .bash_profilethat 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