使用 -regex 和 -exec 查找命令,需要正则表达式帮助

发布于 2024-10-07 03:47:59 字数 466 浏览 4 评论 0原文

我有一个目录,里面有一堆人物图像。不幸的是,图像所来自的系统对于一堆人物图像有其自己的唯一ID,这些图像未在企业的任何其他系统中使用(即客人有一张照片,但实际上并不在员工/客户数据库中...... .就在图片服务器的存储中)。幸运的是,8 个或更多整数后跟扩展名的图片是不需要的(我见过 .bmp 和 .jpg...但可能还有其他)。在将 ls -lh 替换为 rm -f 并编写删除其他系统照片的脚本之前,我已尝试使用以下 find 命令作为测试。我猜我误解了正则表达式,因为通常都是这种情况,但在我看来,以下内容似乎适合我正在寻找的内容。 \d{8,} 似乎不起作用,因为删除它会发现一切都很好,但我不想要一切......只是那些带有 8 个或更多整数的整数,后跟任何内容。我在这里忽略了什么?

find /path/to/dir -regex '\/path\/to\/dir\/\d{8,}.*' -exec ls -lh {} \;

谢谢。

I have a directory with a bunch of person images in it. Unfortunately the system the images are coming from has its own unique IDs for a bunch of person images that are not used in any other systems at the business (ie guests have a picture but aren't actually in the employee/customer db....just in the picture server's storage). Fortunately pictures of 8 or more integers followed by an extension are unwanted (I've seen .bmp and .jpg...but there could be others). I've attempted the following find command as a test before I swap the ls -lh with an rm -f and script the removal of the photos for other systems. I am guessing I am misunderstanding regular expressions, as that is usually the case, but everywhere I look sure seems like the following should work for what I am looking for. The \d{8,} does not appear to be working as removing it finds everything just fine, but I don't want everything....just those with 8 or more integers followed by whatever. What am I overlooking here?

find /path/to/dir -regex '\/path\/to\/dir\/\d{8,}.*' -exec ls -lh {} \;

Thanks.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

九厘米的零° 2024-10-14 03:47:59

我不知道 find 正则表达式类型是否特定于系统。我在 Linux 中看到它有几种类型的正则表达式(POSIX 与 Emacs 不同)。 find 中的默认正则表达式似乎与 emacs 类似。我发现 find 也有一个 -regextype 选项,因此,使用 posix-extend 你可以获得与你想要的类似的东西:

find /path/to/dir -regextype posix-extended -regex '/path/to/dir/[0-9]{8,}.*' -exec ls -lh {} \;

但是这些类型的正则表达式上没有 \d (我认为它们仅来自 Perl 类型的正则表达式),并且不需要 / 的反斜杠。无论如何,这些作品。

I don't know if find regex types are system-specific. I've seen in the Linux one that it has several types of regular expressions (POSIX are different from Emacs). It seems that the default regex in find are emacs-like. I've seen that find also has a -regextype option, so, using the posix-extended you can get something similar to what you want:

find /path/to/dir -regextype posix-extended -regex '/path/to/dir/[0-9]{8,}.*' -exec ls -lh {} \;

but you don't have \d on those type of regexes (I think they come from Perl-type regex only), and don't need backslashes for /. Anyway, these works.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文