bash - 如何使用 "find"通过正则表达式搜索文件名

标签 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
  1. -name 使用globular 表达式, 又名通配符。你想要的是 -正则表达式
  2. 要按照您的意愿使用间隔,您 需要告诉 find 使用 Extended 正则表达式 通过 -regextype posix-extended 标志
  3. 你需要避开经期 因为在正则表达式中,句点有 任何单曲的特殊含义 字符。你想要的是一个 由 \.
  4. 表示的字面句点
  5. 只匹配那些文件 大于 3 天,您需要在号码前加上 + 作为前缀 在 -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

关于bash - 如何使用 "find"通过正则表达式搜索文件名,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5249779/

相关文章:

bash - 在 Bash 中减去两个变量

c++ - unistd.h read() 正在读取更多数据然后被写入

java - 在Unix中使用Java Runtime类查找Jar文件中的类

python - 通过 cron 从 bash 脚本内部运行 python 文件

bash - 从图像为 FFmpeg 生成调色板

string - 变量替换中的 Bash 冒号运算符?

linux - shell 脚本 (AIX) : Extracting OS and browser information from user agent string

mysql - 使用 Windows 提取/加载 Unix .nightly 文件

bash - 如何在 ant 中编写多个 exec 参数

linux - 如何在特定时间复制具有特定后缀的文件?