find . -regex "[a-f0-9\-]\{36\}\.jpg".
但它不工作。正则表达式有问题吗?
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'.
find
命令中正则表达式的工作方式。根据手册,正则表达式匹配整个文件路径,包括目录,这意味着你的正则表达式周围有一个隐含的 "^...$"。它必须匹配整行结果。 - Manny D.*/
中不需要/
,因为.*
可以匹配零个或多个(几乎)任何字符。 - Jeff\{36\}
。 - Lucas Wilson-Richter-regex
标志之前放置-regextype
标志,否则它将无效! - Christopher Orr-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 find的,我不知道BSD版本的find,它也是Mac上的默认版本。)
-regextype
是 GNU find
的选项,而非 BSD(至少不是类似于 Mac 的 BSD)find
。如果该选项不可用,请确保安装 GNU find。如果使用 Mac,则可以通过 brew 包 findutils
进行安装。之后,可以通过 gfind
使用 find。 - DanCatposix-egrep
can be shortened to just egrep
- bloody从其他答案来看,这似乎是find命令的问题。
但是,您可以使用以下方式代替:
find . * | grep -P "[a-f0-9\-]{36}\.jpg"
您可能需要微调grep并根据您想要的内容使用不同的选项,但它可以工作。
find
的 -prune
功能,该功能将完全跳过某些目录。大多数情况下,这并不是很重要,但值得一提。 - Alexander Birdfind
命令的正则表达式语法一直感到困惑,但这个方法解决了我的问题。 - uzluisf在 Mac OS X 上(使用 BSD find):与被接受的回答产生相同的效果。
$ find -E . -regex ".*/[a-f0-9\-]{36}.jpg"
man find
说 -E
使用扩展正则表达式支持
注意:需要使用.*/
前缀来匹配完整的路径:
为了比较,这里是GNU/Linux版本:
$ find . -regextype sed -regex ".*/[a-f0-9\-]\{36\}\.jpg"
find . -regex '\./[a-f0-9-]*.jpg'
$ 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)
dir/
目录下搜索 samplefile.txt
并使用 find . -regex 'samplefile.*'
这种符号表示法,find 将无法工作。 - Elcidegrep
而不是sed
- 所以谢谢。 - dotnetCarpenterfind . -regex "[a-f0-9\-]\{36\}\.jpg"
find . -regex "./[a-f0-9\-]\{36\}\.jpg"
find . -regextype posix-extended -regex "[a-f0-9\-]\{36\}\.jpg"
有一件事我没有看到涵盖,那就是如何将正则表达式与常规查找语法结合起来。
例如:我想在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
所以我正在使用修剪命令来排除多个系统目录,然后对剩余的文件进行正则表达式操作。任何错误输出(stderr)都将被删除。
重要的部分是先使用Find语法,然后使用OR(-o)与正则表达式。
如果您想要保持跨平台兼容性,我找不到任何内置的正则表达式搜索选项可以在不同版本的find
中以一致的方式工作。
与grep结合使用
find . | grep -E '<POSIX regex>'
这可能会很慢,但如果您需要使用完整的正则表达式并且无法将搜索重新格式化为glob,则会提供跨平台正则表达式搜索。
重写为glob
-name
选项与glob兼容,将提供有限(但跨平台)的模式匹配。您可以使用命令行上的所有模式,例如* ? {} **
。虽然不像完整的正则表达式那样强大,但根据您的用例,您可能能够将搜索重新格式化为glob。
互联网搜索globs - 许多详细功能的教程都可以在线获得。