How can I exclude all “permission denied” result lines from “grep”? How can I exclude all “permission denied” result lines from “grep”? unix unix

How can I exclude all “permission denied” result lines from “grep”?


The messages you are receiving is due to a lack of permission on those files, i.e., those are error messages.
All you have to do is to redirect the stderr (standard error output) to /dev/null, like this:

grep -irl "foo" 2> /dev/null

To lear more about redirection (on bash), read this article:Bash Reference Manual - Redirections

Edit: You can also just suppress error messages by using:

grep -irl "foo" 2>&-


I prefer to use the -s 'suppress' flag:

grep -irls "foo"

Note the "Portability note" from the grep man page:

-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.)