grep -v 迁移后不再排除模式

grep -v no longer excluding pattern after migration

我们的一个共享托管站点最近移动了。新服务器是 Red Hat 4.8.5-36。其他二进制文件的版本是 grep (GNU grep) 2.20 和 find (GNU findutils) 4.5.11

此 cron 作业之前至少运行了 6 年,并为我们提供了与日志、缓存等不匹配的更新文件列表。

find /home/example/example.com/public_html/ -mmin -12 \
    | grep -v 'error_log|logs|cache'

移动后 -v 似乎无效,我们得到类似

的结果
/home/example/example.com/public_html/products/cache/ssu/pc/d/5/c

移动后立即发生结果变化。任何人都知道为什么它现在坏了?此外 - 如何恢复过滤后的输出?

如果你喜欢排除一组词

grep -v -e 'error_log' -e 'logs' -e 'cache' file

使用 awk 你可以:

awk '!/error_log|logs|cache/' file

它将排除所有包含这些词的行。

grep -v 'error_log|logs|cache'

仅排除字面上包含 error_log|logs|cache 的字符串。要使用交替,请使用扩展的正则表达式:

grep -Ev 'error_log|logs|cache'

GNU grep 支持交替作为基本正则表达式的扩展,但是 | 需要转义,所以这也可以工作:

grep -v 'error_log\|logs\|cache'

然而,grep 并不是必需的,我们可以使用 (GNU) find 来完成所有工作:

find /home/example/example.com/public_html/ -mmin -12 \
    -not \( -name '*error_log*' -or -name '*logs*' -or -name '*cache*' \)

或者,POSIX 合规:

find /home/example/example.com/public_html/ -mmin -12 \
    \! \( -name '*error_log*' -o -name '*logs*' -o -name '*cache*' \)

或者,如果您的 find 支持 -regex(GNU 和 BSD find 都支持):

find /home/example/example.com/public_html/ -mmin -12 \
    -not -regex '.*\(error_log\|logs\|cache\).*'