如何在Linux / Unix中将多个文件从一个扩展名重命名为另一个扩展名?

时间:2021-05-25 07:09:01

I have a number of files that end in a '.1', for example:

我有一些以'.1'结尾的文件,例如:

example.file.ex1.1
example.file.ex2.1
example.file.ex3.1

Is there a way that I can quickly rename them all without the '.1' at the end (e.g. example.file.ex1, example.file.ex2, etc.)?

有没有办法可以快速重命名它们而不使用末尾的'.1'(例如example.file.ex1,example.file.ex2等)?

Thanks!

谢谢!

3 个解决方案

#1


5  

Pure bash solution:

纯粹的bash解决方案:

for curFile in example.file.*.1; do
    mv -- "$curFile" "${curFile:0:-2}"
done

#2


12  

Yes, try this with rename :

是的,请重命名:

rename -n 's/\.1$//' *

remove the -n (dry-run mode switch) if your tests are valid.

如果您的测试有效,请删除-n(干运行模式开关)。

warning http://pix.toile-libre.org/upload/original/1377510865.png There are other tools with the same name which may or may not be able to do this, so be careful.

警告http://pix.toile-libre.org/upload/original/1377510865.png还有其他同名的工具,可能会也可能不会这样做,所以要小心。


If you run the following command (linux)

如果运行以下命令(linux)

$ file $(readlink -f $(type -p rename))

and you have a result like

你有一个结果

.../rename: Perl script, ASCII text executable

then this seems to be the right tool =)

那么这似乎是正确的工具=)

If not, to make it the default (usually already the case) on Debian and derivative like Ubuntu :

如果没有,为了使它成为Debian上的默认(通常已经是这种情况)和像Ubuntu这样的衍生物:

$ sudo update-alternatives --set rename /path/to/rename

Last but not least, this tool was originally written by Larry Wall, the Perl's dad.

最后但同样重要的是,这个工具最初是由Perl的父亲Larry Wall编写的。

#3


3  

Another solution using parameter expansion:

使用参数扩展的另一个bash解决方案

for curFile in example.file.*.1; do
    mv "$curFile" "${curFile%.1}"
done

#1


5  

Pure bash solution:

纯粹的bash解决方案:

for curFile in example.file.*.1; do
    mv -- "$curFile" "${curFile:0:-2}"
done

#2


12  

Yes, try this with rename :

是的,请重命名:

rename -n 's/\.1$//' *

remove the -n (dry-run mode switch) if your tests are valid.

如果您的测试有效,请删除-n(干运行模式开关)。

warning http://pix.toile-libre.org/upload/original/1377510865.png There are other tools with the same name which may or may not be able to do this, so be careful.

警告http://pix.toile-libre.org/upload/original/1377510865.png还有其他同名的工具,可能会也可能不会这样做,所以要小心。


If you run the following command (linux)

如果运行以下命令(linux)

$ file $(readlink -f $(type -p rename))

and you have a result like

你有一个结果

.../rename: Perl script, ASCII text executable

then this seems to be the right tool =)

那么这似乎是正确的工具=)

If not, to make it the default (usually already the case) on Debian and derivative like Ubuntu :

如果没有,为了使它成为Debian上的默认(通常已经是这种情况)和像Ubuntu这样的衍生物:

$ sudo update-alternatives --set rename /path/to/rename

Last but not least, this tool was originally written by Larry Wall, the Perl's dad.

最后但同样重要的是,这个工具最初是由Perl的父亲Larry Wall编写的。

#3


3  

Another solution using parameter expansion:

使用参数扩展的另一个bash解决方案

for curFile in example.file.*.1; do
    mv "$curFile" "${curFile%.1}"
done