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

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 -n
whatever/.git/svn: No such file or directory

Is there any way I can make those lines go away?

  • git refusing to fetch into current branch
  • Excluding files from Getting rebased
  • Structure in git with multiple websites
  • How can I recover my commit after I did a git reset --hard head?
  • In a Makefile, how can I fetch and assign a git commit hash to a variable?
  • Git Fatal Error: info/refs not found
  • Is it possible to drop a single file from git stash?
  • Amazon secret keys and Heroku
  • git-svn commits to wrong branch / how to merge svn branch via git-svn
  • Deleting a commit in Between other commits
  • Git gets file hungry every once in a while
  • Git: refname 'master' is ambiguous
  • 7 Solutions collect form web for “How can I have grep not print out 'No such file or directory' errors?”

    You can use the -s or --no-messages flag to suppress errors.

    -s, –no-messages suppress error messages

    grep pattern * -s -R -n
    

    If you are grepping through a git repository, I’d recommend you use git grep. You don’t need to pass in -R or the path.

    git grep pattern
    

    That will show all matches from your current directory down.

    Errors like that are usually sent to the “standard error” stream, which you can pipe to a file or just make disappear on most commands:

    grep pattern * -R -n 2>/dev/null
    

    I usually don’t let grep do the recursion itself. There are usually a few directories you want to skip (.git, .svn…)

    You can do clever aliases with stances like that one:

    find . \( -name .svn -o -name .git \) -prune -o -type f -exec grep -Hn pattern {} \;
    

    It may seem overkill at first glance, but when you need to filter out some patterns it is quite handy.

    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
    

    Have you tried the -0 option in xargs? Something like this:

    ls -r1 | xargs -0 grep 'some text'
    

    Use -I in grep.

    Example: grep SEARCH_ME -Irs ~/logs.

    Git Baby is a git and github fan, let's start git clone.