无法重命名名称带有“[ ]”的文件?

无法重命名名称带有“[ ]”的文件?

我一直在尝试在 debian 中使用重命名功能,我在 unix.stackexchange 中搜索,但似乎解决方案在我的场景中包含以下文件:[]

[800p]-[WOLU-H]-test1.mkv
[800p]-[WOLU-H]-test2.mkv

期望的输出只是

[WOLU-H]-test1.mkv
[WOLU-H]-test2.mkv

我试过

rename [800p]-[WOLU-H] [WOLU-H] [800p]-*

但它一直说:

Bareword found where operator expected at (eval 1) line 1, near "800p"
    (Missing operator before p?)
syntax error at (eval 1) line 1, near "800p"

我尝试过其他的,例如

rename 's/[800p]-[WOLU-H]/[WOLU-H]/' [800p]-*

但输出也失败

Invalid [] range "U-H" in regex; marked by <-- HERE in m/[800p]-[WOLU-H <-- HERE ]/ at (eval 1) line 1.

有人可以告诉我正确的过程吗?谢谢!

更新

我试过这个:

rename 's/\[800p\]-\[WOLU-H\]/\[WOLU-H\]/' \[800p\]-*

但错误:

Bareword found where operator expected at (eval 1) line 1, near "800p"
        (Missing operator before p?)
Backslash found where operator expected at (eval 1) line 1, near "p\"
Backslash found where operator expected at (eval 1) line 1, near "]\"
        (Missing operator before \?)
Backslash found where operator expected at (eval 1) line 1, near "]\"
        (Missing operator before \?)
syntax error at (eval 1) line 1, near "800p"
Unmatched right square bracket at (eval 1) line 1, at end of line
Unmatched right square bracket at (eval 1) line 1, at end of line

我也尝试过这个:

rename "[800p]-[WOLU-H]" "[WOLU-H]" "[800p]-"*

但还是报错

Bareword found where operator expected at (eval 1) line 1, near "800p"
        (Missing operator before p?)
syntax error at (eval 1) line 1, near "800p"

我认为-数字很混乱?

答案1

[并且]在 和 正则表达式中也有特殊含义bash,因此您必须将它们转义为\[\]。像这样的东西应该有效:

rename 's/\[800p\]-\[WOLU-H\]/\[WOLU-H\]/' \[800p\]-*

例子:

$ touch [800p]-[WOLU-H]-test1.mkv [800p]-[WOLU-H]-test2.mkv
$ ls
[800p]-[WOLU-H]-test1.mkv  [800p]-[WOLU-H]-test2.mkv
$ rename 's/\[800p\]-\[WOLU-H\]/\[WOLU-H\]/' \[800p\]-*
$ ls
[WOLU-H]-test1.mkv  [WOLU-H]-test2.mkv

答案2

为什么不简单地使用引号呢?所以你不需要逃避那些[

rename "[800p]-[WOLU-H]" "[WOLU-H]" "[800p]-"*

(注意*是在 之外"..."

编辑:评论让我注意到它不起作用。我不明白为什么,因为它在这里工作:

$ ls -1
[800p]-[WOLU-H]-foobar.txt
[800p]-[WOLU-H]-hello.txt
[800p]-[WOLU-H]-world.txt

$ rename "[800p]-[WOLU-H]" "[WOLU-H]" "[800p]-[WOLU-H]"*

$ ls -1
[WOLU-H]-foobar.txt
[WOLU-H]-hello.txt
[WOLU-H]-world.txt

$ rename -V
rename (util-linux-ng 2.17.2)

我正在使用 CentOS 6.6,也许这就是区别...无论如何,抱歉回答错误...

相关内容