将regex与util-linux中的rename版本一起使用 [英] Using regex with `rename` version from `util-linux`
问题描述
我正在使用GNU/Linux发行版,其中实用程序 rename
来自 util-linux
,并且我想充分利用正则表达式(Perl或POSIX)用它.
I’m using a GNU/Linux distribution where the utility rename
comes from util-linux
and I want to make full use of regular (Perl or POSIX) expressions with it.
有两个版本的重命名
:
- "Perl"版本,语法为
重命名's/^ fgh/jkl/'fgh *
-
util-linux
版本,语法为rename fgh jkl fgh *
- The "Perl" version, with syntax
rename 's/^fgh/jkl/' fgh*
- The
util-linux
version, with syntaxrename fgh jkl fgh*
如果在第一个正则表达式中使用正则表达式似乎很明显,那么我就很难使用它.但是,我对第二个表达式感到困惑:我找不到有关正则表达式的可能用法以及格式的格式的任何相关文档或示例.
If the use of regexes seems pretty obvious with the first one, to which I have no easy access. However, I’m confused about the second one: I could not find any relevant documentation or examples on the possible use, and in that case the format, of the regular expressions to use.
让我们举一个简单的例子,该目录包含:
Let’s take, to make a simple example, a directory containing:
foo_a1.ext
foo_a32.ext
foo_c18.ext
foo_h12.ext
我想使用类似于以下两行之一的语法:
I want to use a syntax like one of these two lines:
rename "foo_[a-z]([0-9]{1,2}).ext" "foo_\1.ext" *
rename "foo_[:alpha:]([:digit:]{1,2}).ext" "foo_\1.ext" *
其预期输出将是:
foo_1.ext
foo_32.ext
foo_18.ext
foo_12.ext
这当然行不通!我缺少明显的东西,或者有此工具没有使用实际正则表达式的实现方式.
Of course this does not work! Either I’m missing something obvious, or there is no implemented way to use actual regular expressions with this tool.
(请注意,我知道在shell解释器中使用正则表达式重命名文件的其他可能性;这个问题针对的是 rename
工具的特定版本.)
(Please note that I am aware of the other possibilities for renaming files with regular expressions in a shell interpreter; this question aims at a specific version of the rename
tool.)
推荐答案
以下是手册页:http://linux.die.net/man/1/重命名.这很简单:
Here is the manual page: http://linux.die.net/man/1/rename. It is pretty straightforward:
将从重命名为文件...
rename from to file...
重命名将通过替换第一个来重命名指定的文件 from 在它们的名字中由 to 出现.
rename will rename the specified files by replacing the first occurrence of from in their name by to.
我相信没有正则表达式,只是简单的子字符串匹配.
I believe there are no regexes, it is just plain substring match.
这篇关于将regex与util-linux中的rename版本一起使用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!