【问题标题】:grep -r --include not works but --exclude worksgrep -r --include 不起作用,但 --exclude 起作用
【发布时间】:2021-03-04 19:49:07
【问题描述】:

在 bash 中,

grep -r --exclude={*.c,} 'HELLO'  // exclude c files

grep -r --include={*.c,} 'HELLO'  // just same with grep -r 'HELLO'

有什么问题?

【问题讨论】:

    标签: linux bash grep centos


    【解决方案1】:

    您错误地使用了花括号。正确的语法看起来像

    grep -r --include='*.c' 'HELLO' .
    

    (请注意最后添加了缺少的文件名参数.。)

    您可以通过在前面放置 echo 并检查 shell 将大括号扩展为什么来查看问题所在。

    $ echo grep -r --exclude={*.c,} 'HELLO'
    grep -r --exclude=*.c --exclude= HELLO
    

    【讨论】:

    • GNU grep 将在未给出文件名参数时搜索当前目录,-r 处于活动状态
    • 是的,有很多不可移植的 GNU 便利。我会尽量避免在答案中使用它们,或者指出我使用的东西不便携,即使问题提到了特定平台。
    猜你喜欢
    • 1970-01-01
    • 2017-10-30
    • 1970-01-01
    • 1970-01-01
    • 2014-08-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-06-20
    相关资源
    最近更新 更多