在ZMV REGEX范围过滤器中包括周期字符(“。”)
我正在尝试编写一个简单的 cronjob,通过文件夹和目录进行递归。嵌套在 /OneDrive
中的文件,并替换非 AZ
、az
、的文件或文件夹名称字符0-9
,或 .
加上 _
。 (这让我觉得这是解决同步错误的最简单方法。)
构建 这篇 StackExchange 帖子 我已经成功地使用了其中的大部分方式:
zmv '**/*' '$f:h${${f:t}//[^A-Za-z0-9]/_}'
但是,此正则表达式不排除。
的,这导致我所有文件夹中的所有文件扩展名都更改为 _
(例如 file.txt
变为 file_txt
)。
我距离高级正则表达式用户还很远,并且我使用的该命令的所有各种排列都引发了错误,包括:
zmv '**/*' '$f:h${${f:t}//[^\.A-Za-z0-9]/_}'
zmv '**/*' '$f:h${${f:t}//[^.A-Za-z0-9]/_}'
zmv '**/*' '$f:h${${f:t}//[^A-Za-z0-9\.]/_}'
我确信正确的正则表达式是显而易见的;恐怕这对我来说并不明显。
如果有人可以在这里提供一些指导,并简要解释为什么我之前的尝试不起作用,我将不胜感激,因为它对我对正则表达式的理解做出了小小的贡献。
I'm trying to write a simple cronjob that recurses through the folders & files nested in /OneDrive
and replaces file or folder name characters that are not A-Z
, a-z
, 0-9
, or .
with an _
. (This strikes me as the easiest way to resolve syncing errors.)
Building off this StackExchange post I've managed to get most of the way there using:
zmv '**/*' '$f:h${${f:t}//[^A-Za-z0-9]/_}'
This regex expression, however, does not exclude .
's, which results in all of the file extensions in all of my folders being changed to _
s (e.g. file.txt
becomes file_txt
).
I'm far from an advanced regex user, and all of the various permutations of this command I've used have thrown errors, including:
zmv '**/*' '$f:h${${f:t}//[^\.A-Za-z0-9]/_}'
zmv '**/*' '$f:h${${f:t}//[^.A-Za-z0-9]/_}'
zmv '**/*' '$f:h${${f:t}//[^A-Za-z0-9\.]/_}'
I'm sure the correct regex expression is obvious; I'm afraid it's just not obvious to me.
If anyone could provide some guidance here, along with a brief explanation of why my previous attempts didn't work, I'd be grateful for the small contribution to my understanding of regex.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
:h
修饰符去掉了斜线;这已将其添加回来:我不确定为什么您引用的原始答案中缺少这一点。
一些注释/警告:
zmv
的-n
选项对于追踪此类问题非常有帮助。zmv
可能无法同时重命名文件名和目录名;可能需要多次通过。zmv
使用文件通配模式。通配模式与正则表达式类似,但存在许多显着差异,因此许多正则表达式文档此处不适用。The
:h
modifier stripped out a slash; this has it added back in:I'm not sure why that was missing from the original answer you referenced.
Some notes / caveats:
-n
option forzmv
can be very helpful in tracking down issues like this.zmv
may not be able to rename both file names and directory names; multiple passes could be needed.zmv
uses file globbing patterns. Globbing patterns are similar to regular expressions, but there are a number of significant differences, so a lot of documentation for regexes will not apply here.