在Mac终端上使用Regex重命名文件



我正在尝试重命名一个文件夹中的文件,该文件夹的文件名为:

[A_LOT_OF_TEXT'].pdf&blobcol=url数据&blobtable=MungoBlobs

  • 我基本上想获得".pdf"之前的所有内容,包括".pdf">

安装brew重命名后,我尝试使用它,但它不起作用:

rename -n -v  '.*?(.pdf)' *

我得到错误:

Using expression: sub { use feature ':5.28'; .*?(.pdf) }
syntax error at (eval 2) line 1, near "; ."

有什么解决办法吗?

您当然想要

rename -n -v  's/^(.*?.pdf).*/$1/' *

请参阅正则表达式证明。一旦您确定表达式对您适用,请删除-n

解释

--------------------------------------------------------------------------------
^                        the beginning of the string
--------------------------------------------------------------------------------
(                        group and capture to $1:
--------------------------------------------------------------------------------
.*?                      any character except n (0 or more times
(matching the least amount possible))
--------------------------------------------------------------------------------
.                       '.'
--------------------------------------------------------------------------------
pdf                      'pdf'
--------------------------------------------------------------------------------
)                        end of $1
--------------------------------------------------------------------------------
.*                       any character except n (0 or more times
(matching the most amount possible))

我建议您查看手册页中重命名的示例用法
一种可能的解决方案如下:

rename -v -n 's/(.*pdf).*/$1/' *

说明:

-v:打印重命名成功的文件名
-n:试运行;不要实际重命名
$1:匹配正则表达式中的第一组
*:在目录中的所有文件上运行

请在此处阅读有关替代命令的更多信息。

最新更新