62

So, the thing is, I'm on linux terminal using grep command and I want the output without all the lines where it prints at the beginning "grep:" or the lines that begins with "./", because now I'm getting something like this:

grep: ./users/blabla1: Permission denied
grep: ./users/blabla2: Permission denied
grep: ./users/blabla3: Permission denied
grep: ./users/blabla4: Permission denied
grep: ./users/blabla5: Permission denied
grep: ./users/blabla6: Permission denied
grep: ./users/blabla7: Permission denied
grep: ./users/blabla8: Permission denied
./foo/bar/log.log
./foo/bar/xml.xml

I have tried this:

grep -irl "foo" . | grep -v "Permission denied"

I have also tried this one:

 grep -irl "foo" . | grep -v "^grep:"

And finally this one:

 grep -irl "foo" . | grep "^./"

But I keep getting same results as if I haven't put anything after the |, any ideas? What am I missing?

Nimantha
  • 6,405
  • 6
  • 28
  • 69
gleba
  • 703
  • 1
  • 7
  • 8

3 Answers3

64

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>&-
gender_madness
  • 1,080
  • 11
  • 13
  • I haven't explained myself clearly, thank you very much by the way. I know that I don't have permission, and I can't get it. The thing is, I don't know the results of the grep, not all of them are in `./foo`, they could be anywhere, I can get `./foo` or `./bar`, I just want to discard all the results that shows I don't have permission. – gleba Mar 23 '16 at 15:21
  • 1
    @gleba, this is what the redirection is supposed to do. All the results (`stdout`) will be displayed except for the `Permission denied` entries (`stderr`). You can re-test your command just adding `2> /dev/null` at the end – gender_madness Mar 23 '16 at 17:20
  • 1
    And thus with this perfectly clear answer I finally understood what `2>` means in terminal (it's hard to search) – MichaelChirico Apr 23 '19 at 03:39
  • Not a good solution because it will exclude all errors, not just the permission errors. OP is wants to hide permission errors, but there may be other errors he wants to see. – Jessica Dec 26 '22 at 22:19
64

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

Community
  • 1
  • 1
mhck
  • 873
  • 1
  • 10
  • 20
  • 5
    I think this is a better answer for the requirement. You can search -s flag in official documentation [link](https://www.gnu.org/software/grep/manual/grep.html) – shaurya airi Feb 25 '20 at 10:36
  • 6
    A tip for remembering this is that you are looking for "girls" called "foo": grep -irls "foo" – thlik Oct 27 '20 at 13:11
2

Going off of your first try:

grep -irl "foo" . | grep -v "Permission denied"

You're just missing the operator to redirect standard error. '|' redirects standard output. if you add '&', you will also redirect standard error, which is what is giving you the "Permission denied" message. Try:

grep -irl "foo" . &| grep -v "Permission denied"

This works for me, because for some reason my machine doesn't like the "2> /dev/null" option.

skittlebiz
  • 359
  • 4
  • 8