【发布时间】:2011-03-09 17:32:46
【问题描述】:
我试图查找所有日期为 3 天或更早的文件。
find /home/test -name 'test.log.\d{4}-d{2}-d{2}.zip' -mtime 3
它没有列出任何东西。它有什么问题?
【问题讨论】:
我试图查找所有日期为 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 使用 globular 表达式,
又名通配符。你想要的是
-regex
find 使用扩展
正则表达式通过
-regextype posix-extended标志\. 表示的文字句点
+ 作为前缀
在-mtime +3。$ find . -regextype posix-extended -regex '^.*test\.log\.[0-9]{4}-[0-9]{2}-[0-9]{2}\.zip'
./test.log.1234-12-12.zip
【讨论】:
-E。 find -E . -regex 'theregex' 利用扩展(现代)正则表达式。
[0-9]{2} 在 macOS 上对我不起作用,我不得不使用 [0-9][0-9]
find -E . -iregex '^.*\.js(\.map)?$'
开始于:
find . -name '*.log.*.zip' -a -mtime +1
你可能不需要正则表达式,试试:
find . -name '*.log.*-*-*.zip' -a -mtime +1
您需要 +1 以匹配 1、2、3 ...
【讨论】:
使用-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'
【讨论】:
使用 -regex 而不是 -name,并注意正则表达式与 find 将打印的内容相匹配,例如“/home/test/test.log”不是“test.log”
【讨论】:
只是对搜索目录和文件的正则表达式进行了少许阐述
找一个名字像书的目录
find . -name "*book*" -type d
找一个名字像书字的文件
find . -name "*book*" -type f
【讨论】: