tho*_*lin 268 regex linux find
我有一些用生成的uuid1字符串命名的图像.例如81397018-b84a-11e0-9d2a-001b77dc0bed.jpg.我想使用"find"命令找出所有这些图像:
find . -regex "[a-f0-9\-]\{36\}\.jpg".
Run Code Online (Sandbox Code Playgroud)
但它不起作用.正则表达式出了什么问题?有人可以帮我吗?
Sus*_*Pal 318
find . -regextype sed -regex ".*/[a-f0-9\-]\{36\}\.jpg"
Run Code Online (Sandbox Code Playgroud)
请注意,您需要.*/
在开头指定,因为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
Run Code Online (Sandbox Code Playgroud)
我的发现版本:
$ 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'.
Run Code Online (Sandbox Code Playgroud)
Paŭ*_*ann 74
该-regex
发现表达式匹配的全名,包括从当前目录的相对路径.为此,find .
始终以./
任何目录开头.
此外,这些是emacs
正则表达式,其具有除通常的egrep正则表达式之外的其他转义规则.
如果这些都直接在当前目录中,那么
find . -regex '\./[a-f0-9\-]\{36\}\.jpg'
Run Code Online (Sandbox Code Playgroud)
应该管用.(我不太确定 - 我无法在这里得到重复的计数.)你可以通过-regextype posix-egrep
以下方式切换到egrep表达式:
find . -regextype posix-egrep -regex '\./[a-f0-9\-]{36}\.jpg'
Run Code Online (Sandbox Code Playgroud)
(请注意,这里所说的一切都是针对GNU查找的,我对BSD一无所知,这也是Mac上的默认设置.)
yar*_*ian 31
从其他答案来看,似乎这可能是错误的.
但是你可以这样做:
find . * | grep -P "[a-f0-9\-]{36}\.jpg"
您可能需要稍微调整一下grep并根据您的需要使用不同的选项,但它可以正常工作.
尝试使用单引号(')来避免shell转义为字符串.请记住,表达式需要匹配整个路径,即需要看起来像:
find . -regex '\./[a-f0-9-]*.jpg'
Run Code Online (Sandbox Code Playgroud)
除此之外,我的发现(GNU 4.4.2)似乎只知道基本的正则表达式,特别是{36}语法.我想你必须没有它.
简单方法-您可以在开头指定。*,因为find与整个路径匹配。
$ find . -regextype egrep -regex '.*[a-f0-9\-]{36}\.jpg$'
Run Code Online (Sandbox Code Playgroud)
查找版本
$ 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)
Run Code Online (Sandbox Code Playgroud)
小智 6
在使用正则表达式应用查找指令时,应使用绝对目录路径.在你的例子中,
find . -regex "[a-f0-9\-]\{36\}\.jpg"
Run Code Online (Sandbox Code Playgroud)
应改成
find . -regex "./[a-f0-9\-]\{36\}\.jpg"
Run Code Online (Sandbox Code Playgroud)
在大多数Linux系统中,正则表达式中的某些规则无法被该系统识别,因此您必须明确指出-regexty
find . -regextype posix-extended -regex "[a-f0-9\-]\{36\}\.jpg"
Run Code Online (Sandbox Code Playgroud)
在Mac OS X上(找到BSD):与接受的答案相同,.*/
需要前缀以匹配完整路径:
$ find -E . -regex ".*/[a-f0-9\-]{36}.jpg"
Run Code Online (Sandbox Code Playgroud)
man find
说-E
使用扩展的正则表达式支持
归档时间: |
|
查看次数: |
360084 次 |
最近记录: |