Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/unix/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Bash 如何在文件查找中使用正则表达式_Bash_Unix - Fatal编程技术网

Bash 如何在文件查找中使用正则表达式

Bash 如何在文件查找中使用正则表达式,bash,unix,Bash,Unix,我试图找到所有的文件日期和所有文件3天前或以上 find /home/test -name 'test.log.\d{4}-d{2}-d{2}.zip' -mtime 3 它没有列出任何东西。怎么了 find /home/test -regextype posix-extended -regex '^.*test\.log\.[0-9]{4}-[0-9]{2}-[0-9]{2}\.zip' -mtime +3 -name使用球状表达式, 又名通配符。你想要的是 -regex 要按您的意愿使用

我试图找到所有的文件日期和所有文件3天前或以上

find /home/test -name 'test.log.\d{4}-d{2}-d{2}.zip' -mtime 3
它没有列出任何东西。怎么了

find /home/test -regextype posix-extended -regex '^.*test\.log\.[0-9]{4}-[0-9]{2}-[0-9]{2}\.zip' -mtime +3
  • -name
    使用球状表达式, 又名通配符。你想要的是
    -regex
  • 要按您的意愿使用间隔,您需要 需要告诉
    find
    使用扩展 通过
    -regextype posix extended
    标志
  • 你需要避开经期 因为在正则表达式中,句点具有 任何一项的特殊含义 性格你想要的是一个 用
    \.
  • 仅匹配以下文件: 超过3天,您需要在您的号码前面加上
    +
    作为前缀 在
    -mtime+3
  • 概念证明
    使用-regex not-name,并注意regex与find将打印的内容相匹配,例如“/home/test/test.log”而不是“test.log”

    使用
    -regex

    从手册页:

    -regex pattern
           File name matches regular expression pattern.  This is a match on the whole path, not a search.  For example, to match a file named './fubar3',  you  can  use  the
           regular expression '.*bar.' or '.*b.*3', but not 'b.*r3'.
    
    另外,我不相信
    find
    支持正则表达式扩展,比如
    \d
    。您需要使用
    [0-9]

    find . -regex '.*test\.log\.[0-9][0-9][0-9][0-9]-[0-9][0-9]-[0-9][0-9]\.zip'
    
    首先:

    find . -name '*.log.*.zip' -a -mtime +1
    
    您可能不需要正则表达式,请尝试:

     find . -name '*.log.*-*-*.zip' -a -mtime +1
    

    您需要+1来匹配1、2、3…

    只需对regex进行一些细化,即可搜索目录和文件

    找到一个名为book的directroy

    find . -name "*book*" -type d
    
    查找名为book word的文件

    find . -name "*book*" -type f
    

    如果您碰巧在OSX上运行,那么请使用
    -E
    <代码>查找-E-regex'theregex'利用扩展(现代)正则表达式。
    [0-9]{2}
    在macOs上对我不起作用,我不得不使用
    [0-9][0-9]
    需要扩展才能使用可选组,即
    find-e-iregex'^.*\.js(\.map)?$'
    @Chris see'-regextype posix extended'@SiegeX我之前的评论是,如果有人知道为了使用可选组,您需要启用posix extended,并给出一个类似的示例,这可能会很有用。感谢您的回答。感谢您和@dogbane指出需要进行完整路径匹配。
    find . -name "*book*" -type f