【问题标题】:How to use -regex in the find command on Linux command line如何在 Linux 命令行的 find 命令中使用 -regex
【发布时间】:2019-12-24 04:06:06
【问题描述】:

目标是查找并列出开头带有“messages”和/或“error.log”等的任何内容,然后列出“messages.1..99”和“error.log.1..99” " 使用正则表达式。

此命令适用于但是,它需要我进行多次 -or 搜索,但为了简化,我希望在搜索中的集合中有多个。比如:

# find /var/log -maxdepth 1 -type f -size +1M -name [messages|error.log|secure.log|kern.log...]?[0-9]|[0-9][0-9] ! -iname "*.gz"

不是

# find /var/log -maxdepth 1 -type f -size +1M -name "messages?[0-9]" -o -name "messages?[0-9][0-9]"

如何使用正则表达式执行此命令?

# find /var/log -maxdepth 1 -type f -size +1M -name "[messages,error.log,kern,secure]?[0-9]" ! -iname "*.gz"

我使用正则表达式的尝试没有在标准输出中打印任何内容:

# find /var/log -maxdepth 1 -type f -size +1M -regex -name "[messages,error,kern,secure]?[0-9]" ! -iname "*.gz"

【问题讨论】:

  • 那(没有找到任何东西)将有两个原因:a)[messages,error,kern,secure] 是一个奇怪的字符类,而不是你认为的那样,b)没有什么可以匹配实际找到的 @987654326 @ 在文件名的开头......你也不能像这样使用-regex -name
  • 正则表达式匹配一切,整个路径,你也需要匹配/var/log/
  • 由于您的复杂查询不起作用,降低复杂度!例如,尝试使用-name messages 而不是-name "[messages,error,kern,secure]?[0-9]"。然后你可能会怀疑你应该看看如何使用-regex

标签: linux bash shell rhel


【解决方案1】:

试试这个:

find /var/log -maxdepth 1 -type f -size +1M -type f -regextype egrep -regex '.*(messages|error|kern|secure)\.[0-9]+.*' -not -name \*gz

【讨论】:

  • find /var/log -maxdepth 1 -type f -size +1M -regextype egrep -regex '.*(messages|error|kern|secure)(\.log)?\.[[:digit:]]+.*' -not -name '*gz'
猜你喜欢
  • 2012-03-14
  • 1970-01-01
  • 1970-01-01
  • 2017-03-16
  • 1970-01-01
  • 2015-09-07
  • 2021-12-05
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多