简体   繁体   English

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

[英]How to rename multiple files from one extension to another in Linux / Unix?

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 (eg example.file.ex1, example.file.ex2, etc.)? 有没有一种方法可以快速重命名它们而不使用末尾的'.1'(例如example.file.ex1,example.file.ex2等)?

Thanks! 谢谢!

Yes, try this with rename : 是的,尝试重命名

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

remove the -n (dry-run mode switch) if your tests are valid. 如果您的测试有效,请删除-n (干运行模式开关)。

警告 There are other tools with the same name which may or may not be able to do this, so be careful. 还有其他具有相同名称的工具可能会或可能不会这样做,所以要小心。


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编写的。

Pure bash solution: 纯粹的bash解决方案:

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

Another solution using parameter expansion : 使用参数扩展的另一个解决方案

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM