【问题标题】:Print the matching line as well as file name打印匹配的行以及文件名
【发布时间】:2024-01-21 05:12:01
【问题描述】:
find . -name "*.sh" -type f -exec grep -l "mail" {} \;

我正在使用上面的语句在sh 文件类型中查找字符串mail 的出现。我还想在该文件中打印字符串 mail 的用法以及文件名。怎么可能?

【问题讨论】:

    标签: shell grep find


    【解决方案1】:

    如果您还想查看行号并过滤掉搜索过程中可能遇到的任何错误(例如,Operation not allowed 错误),您可以执行类似的操作。

    find . -name '*.sh' -type f -exec grep -n mail 2>/dev/null {} +
    

    【讨论】: