Grep, ignore warnings

Those warnings are directed to the stderr stream, as opposed to the standard out file descriptor. You can silence the stderr output by adding 2>/dev/null to the end of your command.


More directly than filtering the warnings you can disable them by adding -s:

grep "My term" -sir --exclude-dir="\.svn" --include=*.{cpp,h} ./

There are some compatibility issues with this option. However, this shouldn't be a problem for personal use.

-s, --no-messages: Suppress error messages about nonexistent or unreadable files. Portability note: unlike GNU grep, 7th Edition Unix grep did not conform to POSIX, because it lacked -q and its -s option behaved like GNU grep's -q option. USG-style grep also lacked -q but its -s option behaved like GNU grep. Portable shell scripts should avoid both -q and -s and should redirect standard and error output to /dev/null instead. (-s is specified by POSIX.)


I used to get a ton of annoying messages like this:

grep: commands: Is a directory
grep: events: Is a directory
grep: views: Is a directory

The reason is that the --directories flag is defaulted to read. I changed it to recurse; if you don't want it to automatically do a recursive search you can use skip instead.

The easiest way to handle this all the time is to set it in an environment variable. In ~/.bash_profile or ~/.bashrc depending on your distro:

export GREP_OPTIONS='--directories=recurse'

Now it automatically suppresses those messages any time I use grep.

Another option is the --no-messages flag, shorthand -s. This will also get rid of the Is a directory messages, but it also suppresses other messages which might be more useful. For example, if you're doing a nested search in */*/* and no such file of that pattern exists, it won't tell you that.

Tags:

Search

Grep