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

Grep

Grep Problem Overview


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?

Grep Solutions


Solution 1 - Grep

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

> -s, --no-messages suppress error messages

grep pattern * -s -R -n

Solution 2 - Grep

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.

Solution 3 - Grep

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

Solution 4 - Grep

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

Solution 5 - Grep

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.

Solution 6 - Grep

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

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

Solution 7 - Grep

Use -I in grep.

Example: grep SEARCH_ME -Irs ~/logs.

Solution 8 - Grep

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"

Solution 9 - Grep

I was getting lots of these errors running "M-x rgrep" from Emacs on Windows with /Git/usr/bin in my PATH. Apparently in that case, M-x rgrep uses "NUL" (the Windows null device) rather than "/dev/null". I fixed the issue by adding this to .emacs:

;; Prevent issues with the Windows null device (NUL)
;; when using cygwin find with rgrep.
(defadvice grep-compute-defaults (around grep-compute-defaults-advice-null-device)
  "Use cygwin's /dev/null as the null-device."
  (let ((null-device "/dev/null"))
	ad-do-it))
(ad-activate 'grep-compute-defaults)

Solution 10 - Grep

One easy way to make grep return zero status all the time is to use || true

echo "Hello" | grep "This won't be found" || trueecho $?
   0

As you can see the output value here is 0 (Success)

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionalexgolecView Question on Stackoverflow
Solution 1 - GrepDogbertView Answer on Stackoverflow
Solution 2 - GrepSteve PrenticeView Answer on Stackoverflow
Solution 3 - GreplunixbochsView Answer on Stackoverflow
Solution 4 - GrepIsaac UribeView Answer on Stackoverflow
Solution 5 - GrepcadrianView Answer on Stackoverflow
Solution 6 - GrepcokedudeView Answer on Stackoverflow
Solution 7 - GrepBalaView Answer on Stackoverflow
Solution 8 - GreptalleyhoView Answer on Stackoverflow
Solution 9 - GrepbeasleraView Answer on Stackoverflow
Solution 10 - GrepNikhil JSKView Answer on Stackoverflow