linux 如何在find命令中使用正则表达式?

cld4siwp  于 2023-08-03  发布在  Linux
关注(0)|答案(9)|浏览(119)

我有一些图像命名为生成的uuid 1字符串。例如81397018-b84a-11e0-9d2a-001b77dc0bed.jpg。我想找出所有这些图像使用“查找”命令:

find . -regex "[a-f0-9\-]\{36\}\.jpg".

字符串
但这不管用。正则表达式有问题吗?有人能帮我一下吗?

pgvzfuti

pgvzfuti1#

find . -regextype sed -regex ".*/[a-f0-9\-]\{36\}\.jpg"

字符串
请注意,您需要在开始时指定.*/,因为find匹配整个路径。
示例如下:

susam@nifty:~/so$ find . -name "*.jpg"
./foo-111.jpg
./test/81397018-b84a-11e0-9d2a-001b77dc0bed.jpg
./81397018-b84a-11e0-9d2a-001b77dc0bed.jpg
susam@nifty:~/so$ 
susam@nifty:~/so$ find . -regextype sed -regex ".*/[a-f0-9\-]\{36\}\.jpg"
./test/81397018-b84a-11e0-9d2a-001b77dc0bed.jpg
./81397018-b84a-11e0-9d2a-001b77dc0bed.jpg


我的find版本:

$ find --version
find (GNU findutils) 4.4.2
Copyright (C) 2007 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.

Written by Eric B. Decker, James Youngman, and Kevin Dalley.
Built using GNU gnulib version e5573b1bad88bfabcda181b9e0125fb0c52b7d3b
Features enabled: D_TYPE O_NOFOLLOW(enabled) LEAF_OPTIMISATION FTS() CBO(level=0) 
susam@nifty:~/so$ 
susam@nifty:~/so$ find . -regextype foo -regex ".*/[a-f0-9\-]\{36\}\.jpg"
find: Unknown regular expression type `foo'; valid types are `findutils-default', `awk', `egrep', `ed', `emacs', `gnu-awk', `grep', `posix-awk', `posix-basic', `posix-egrep', `posix-extended', `posix-minimal-basic', `sed'.

abithluo

abithluo2#

-regex find表达式匹配全名,包括当前目录的相对路径。对于find .,总是从./开始,然后是任何目录。
此外,这些是emacs正则表达式,与通常的egrep正则表达式相比,它有其他转义规则。
如果这些都直接在当前目录中,那么

find . -regex '\./[a-f0-9\-]\{36\}\.jpg'

字符串
应该可以(我不太确定--我不能让计数的重复在这里工作。)你可以通过-regextype posix-egrep切换到egrep表达式:

find . -regextype posix-egrep -regex '\./[a-f0-9\-]{36}\.jpg'


(Note这里说的一切都是GNU find的,我不知道任何关于BSD的,它也是Mac上的默认。

h7wcgrx3

h7wcgrx33#

从其他答案来看,这似乎是芬的错。
你可以这样做,而不是:
第一个月
你可能需要稍微调整一下grep,并根据你的需要使用不同的选项,但它确实有效。

deyfvvtc

deyfvvtc4#

在Mac OS X上(BSD查找):效果与the accepted answer相同。

$ find -E . -regex ".*/[a-f0-9\-]{36}.jpg"

字符串
man find说明-E使用扩展的正则表达式支持
注意:需要.*/前缀来匹配完整的路径:
出于比较的目的,这里是GNU/Linux版本:

$ find . -regextype sed -regex ".*/[a-f0-9\-]\{36\}\.jpg"

t2a7ltrp

t2a7ltrp5#

简单的方法-你可以在开始时指定.*,因为find匹配整个路径。

$ find . -regextype egrep -regex '.*[a-f0-9\-]{36}\.jpg$'

字符串
查找版本

$ find --version
find (GNU findutils) 4.6.0
Copyright (C) 2015 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later 
<http://gnu.org/licenses/gpl.html>.
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.

Written by Eric B. Decker, James Youngman, and Kevin Dalley.
Features enabled: D_TYPE O_NOFOLLOW(enabled) LEAF_OPTIMISATION 
FTS(FTS_CWDFD) CBO(level=2)

qxgroojn

qxgroojn6#

尝试使用单引号(')来避免shell转义字符串。记住表达式需要匹配整个路径,即需要看起来像:

find . -regex '\./[a-f0-9-]*.jpg'

字符串
除此之外,我的发现(GNU 4.4.2)似乎只知道基本的正则表达式,尤其是不知道{36}语法。我想你得将就一下了。

lymnna71

lymnna717#

在应用带有正则表达式的查找指令时,应使用绝对目录路径。在您的示例中

find . -regex "[a-f0-9\-]\{36\}\.jpg"

字符串
应该改成

find . -regex "./[a-f0-9\-]\{36\}\.jpg"


在大多数Linux系统中,正则表达式中的一些规则不能被系统识别,因此您必须显式地指出-regexty,如

find . -regextype posix-extended -regex "[a-f0-9\-]\{36\}\.jpg"

fcipmucu

fcipmucu8#

如果你想保持跨平台的兼容性,我找不到一个内置的regex搜索选项,它可以以一致的方式在不同版本的find上工作。

与grep合并

1.正如@yarian所建议的,你可以运行一个过度包含的find,然后通过grep运行输出:
find . | grep -E '<POSIX regex>'
这可能会很慢,但如果您需要使用完整的正则表达式并且无法将搜索格式重新设置为 glob,则可以为您提供跨平台的正则表达式搜索

重写为glob

  1. -name选项与glob兼容,后者将提供有限的(但跨平台的)模式匹配。
    您可以在命令行中使用所有模式,如* ? {} **。尽管不如完整的正则表达式强大,但您可以根据您的用例将搜索重新格式化为globs。
    在互联网上搜索 globs -许多详细说明完整功能的教程可以在线获得
nqwrtyyt

nqwrtyyt9#

有一件事我没有看到涉及到的是如何合并正则表达式与正则查找语法。
例如:我想在BSD / Linux上找到核心转储文件,我更改为我想扫描的根目录。例如:cd /然后执行:

find \( -path "./dev" -o -path "./sys" -o -path "./proc" \) -prune -o -type f -regextype sed -regex ".*\.core$" -exec du -h {} \; 2> /dev/null

字符串
因此,我使用prune命令排除多个系统目录,然后对其余文件执行正则表达式。删除任何错误输出(stderr)。
重要的部分是首先使用Find语法,然后使用正则表达式的OR(-o)。

相关问题