bash 使用正则表达式使用 /usr/bin/rename 重命名多个文件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7795400/
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-18 00:58:12  来源:igfitidea点击:

Rename multiple files with /usr/bin/rename using regex

regexlinuxbashrename

提问by woobert

I have a lot of pdfs that I want to rename with /usr/bin/rename.

我有很多要重命名的 pdf 文件/usr/bin/rename

The files are named in the following pattern:

这些文件以以下模式命名:

<rating> <a pretty long title> (<author> <year>).pdf
e.g.: +++ The discovery of some very interesting stuff (Dude 1999).pdf

rating: 1 to 5 '+' signs
year: numerical

They should be renamed into the following pattern:

它们应重命名为以下模式:

<author>, <year> <rating> <a pretty long title>.pdf
e.g.: Dude, 1999 +++ The discovery of some very interesting stuff.pdf

I tried to use /usr/bin/renameand wrote this command:

我尝试使用/usr/bin/rename并编写了以下命令:

rename  's/(.*)\ (.*)\ \((.*)\ (.*)\).pdf/,   .pdf/' *.pdf

However, the command does not consider that the rating always contains '+' signs and that the year is always numerical. How can I achieve this? I tried something like ([+]{1,5})and ([0-9]{4}), but it didn't work.

但是,该命令并不认为评级总是包含“+”号并且年份总是数字。我怎样才能做到这一点?我尝试了类似([+]{1,5})和的东西([0-9]{4}),但没有用。

Is renameactually able to interpret something other than (.*)as the input for the variables $1 ... $n?

rename除了(.*)作为变量的输入之外,实际上是否能够解释其他内容$1 ... $n

Thanks for your help!

谢谢你的帮助!

回答by Mark Longair

This works fine for me:

这对我来说很好用:

rename 's/(\+{1,5}) (.*) \((.*) ([0-9]{4})\).pdf/,   .pdf/' -- \
    'Dude, 1999 +++ The discovery of some very interesting stuff.pdf'

... however your question doesn't quote the error message, so it's hard to tell what might be wrong in your situation.

...但是您的问题没有引用错误消息,因此很难判断您的情况可能有什么问题。

Just as a warning, there are two different versions of /usr/bin/renamethat are widely found on Linux systems, and which have different syntaxes. I assume that you are using the Perl one, however, since your original command worked at all. That means that you can use any Perl expression to modify the name - see perlrefor more details.

作为警告,/usr/bin/rename在 Linux 系统上广泛存在两种不同的版本,它们具有不同的语法。但是,我假设您使用的是 Perl,因为您的原始命令完全有效。这意味着您可以使用任何 Perl 表达式来修改名称 -有关更多详细信息,请参阅perlre

回答by Andrii Danyleiko

Unfortunately Fedora (it's my distro) has worthless version of rename.

不幸的是,Fedora(这是我的发行版)有一个毫无价值的重命名版本。

But I have changed it for perl version of replace utility.

但是我已经为替换实用程序的 perl 版本更改了它。

You can find it at CPANget and untar archive and then:

你可以在CPANget and untar archive找到它,然后:

# ./Build installdeps
# sudo ./Build install

!!! It actions replace original fedora rename: bin file and manual, but it can be reverted by yum reinstall and may be reverted at next fedora update Also you can install it separately or use alternatives.

!!!它的作用是取代原来的 fedora 重命名:bin 文件和手册,但它可以通过 yum 重新安装恢复,并且可能在下一次 fedora 更新时恢复你也可以单独安装或使用替代品。