首页 文章

如何使用正则表达式与find命令?

提问于
浏览
221

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

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

但它不起作用 . 正则表达式出了什么问题?有人可以帮我吗?

7 回答

  • 261

    简单的方法 - 你可以在开头指定 . *因为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)
    
  • 1

    从其他答案来看,似乎这可能是找错了 .

    但是你可以这样做:

    find . * | grep -P "[a-f0-9\-]{36}\.jpg"

    您可能需要稍微调整一下grep并根据您的需要使用不同的选项,但它可以正常工作 .

  • 6

    -regex find表达式与 whole name 匹配,包括当前目录的相对路径 . 对于 find . ,这始终以 ./ 开头,然后是任何目录 .

    此外,这些是 emacs 正则表达式,其具有除通常的egrep正则表达式之外的其他转义规则 .

    如果这些都直接在当前目录中,那么

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

    应该管用 . (我'm not really sure - I can' t计算重复在这里工作 . )你可以通过 -regextype posix-egrep 切换到egrep表达式:

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

    (请注意,这里所说的一切都是针对GNU查找的,我对BSD一无所知,这也是Mac上的默认设置 . )

  • 3
    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'.
    
  • 62

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

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

    除此之外,似乎我的find(GNU 4.4.2)只知道基本的正则表达式,特别是{36}语法 . 我想你将不得不做它 .

  • 27

    在使用正则表达式应用查找指令时,应使用绝对目录路径 . 在你的例子中,

    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"
    
  • 6

    on Mac (BSD find) :与接受的答案相同,需要 .*/ 前缀来匹配完整路径:

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

    man find-E 使用扩展的正则表达式支持

相关问题