How can I have grep not print out 'No such file or directory' errors?

后端 未结 9 953
情书的邮戳
情书的邮戳 2020-11-28 19:06

I\'m grepping through a large pile of code managed by git, and whenever I do a grep, I see piles and piles of messages of the form:

> grep pattern * -R -         


        
相关标签:
9条回答
  • 2020-11-28 19:42

    I have seen that happening several times, with broken links (symlinks that point to files that do not exist), grep tries to search on the target file, which does not exist (hence the correct and accurate error message).

    I normally don't bother while doing sysadmin tasks over the console, but from within scripts I do look for text files with "find", and then grep each one:

    find /etc -type f -exec grep -nHi -e "widehat" {} \;
    

    Instead of:

    grep -nRHi -e "widehat" /etc
    
    0 讨论(0)
  • 2020-11-28 19:47

    Use -I in grep.

    Example: grep SEARCH_ME -Irs ~/logs.

    0 讨论(0)
  • 2020-11-28 19:49

    I redirect stderr to stdout and then use grep's invert-match (-v) to exclude the warning/error string that I want to hide:

    grep -r <pattern> * 2>&1 | grep -v "No such file or directory"
    
    0 讨论(0)
提交回复
热议问题