我有一些图像命名与生成uuid1字符串。例如81397018-b84a-11e0-9d2a-001b77dc0bed.jpg。我想用“find”命令找到所有这些图像:
find . -regex "[a-f0-9\-]\{36\}\.jpg".
但这并不奏效。正则表达式有问题吗?有人能帮我一下吗?
我有一些图像命名与生成uuid1字符串。例如81397018-b84a-11e0-9d2a-001b77dc0bed.jpg。我想用“find”命令找到所有这些图像:
find . -regex "[a-f0-9\-]\{36\}\.jpg".
但这并不奏效。正则表达式有问题吗?有人能帮我一下吗?
当前回答
简单的方法-你可以在开头指定.*,因为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)
其他回答
尝试使用单引号(')来避免字符串的shell转义。记住,表达式需要匹配整个路径,即需要看起来像:
find . -regex '\./[a-f0-9-]*.jpg'
除此之外,我的find (GNU 4.4.2)似乎只知道基本的正则表达式,尤其是{36}语法。我想你只能将就着不戴了。
当使用正则表达式应用查找指令时,应该使用绝对目录路径。 在你的例子中,
find . -regex "[a-f0-9\-]\{36\}\.jpg"
应该改为
find . -regex "./[a-f0-9\-]\{36\}\.jpg"
在大多数Linux系统中,正则表达式中的某些规则不能被该系统识别,因此必须显式地指出-regexty like
find . -regextype posix-extended -regex "[a-f0-9\-]\{36\}\.jpg"
regex find表达式匹配整个名称,包括当前目录的相对路径。为了寻找。它总是以。/开头,然后是任何目录。
此外,这些是emacs正则表达式,它们具有与通常的egrep正则表达式不同的其他转义规则。
如果这些都直接在当前目录中,那么
find . -regex '\./[a-f0-9\-]\{36\}\.jpg'
应该工作。(我不是很确定——我不能让计数重复在这里工作。)您可以通过-regextype posix-egrep切换到egrep表达式:
find . -regextype posix-egrep -regex '\./[a-f0-9\-]{36}\.jpg'
(请注意,这里所说的一切都是GNU的,我不知道BSD的任何东西,这也是Mac的默认设置。)
从其他答案来看,这似乎是find的错。
但是你可以这样做:
找到。* | grep - p "[a-f0-9\-]{36}\.jpg"
您可能需要稍微调整grep,并根据需要使用不同的选项,但它是有效的。
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 --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'.