Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?

like image 632
gleba Avatar asked Mar 15 '16 16:03

gleba


People also ask

How do I exclude results from grep?

Exclude Words and Patterns By default, grep is case-sensitive. This means that the uppercase and lowercase characters are treated as distinct. To ignore the case when searching, invoke grep with the -i option. If the search string includes spaces, you need to enclose it in single or double quotation marks.

How do I ignore permissions to Denied in Linux?

How to Exclude All “Permission denied” messages When Using Find Command in UNIX/LINUX? use 2>/dev/null. The 2>/dev/null at the end of the find command tells your shell to redirect the standard error messages to /dev/null, so you won't see them on screen.

Which command will find a file without showing permission denied?

When find tries to search a directory or file that you do not have permission to read the message "Permission Denied" will be output to the screen. The 2>/dev/null option sends these messages to /dev/null so that the found files are easily viewed.

How do I ignore error in find command?

How can you suppress warnings or errors, and only get search result in find command? The find command sends any error or warning message to standard error output (i.e., stderr ). So all you have to do to suppress these messages is to redirect stderr to /dev/null .


1 Answers

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>&- 
like image 94
gender_madness Avatar answered Sep 28 '22 23:09

gender_madness