0

I'm very familiar with using grep with the pipe to exclude everything that doesn't match a pattern. But can I use grep to exclude everything that DOES match a pattern? I thought this would work, the

-v

option, like below:

find /media -name "somenamehere" | grep -v "Permission denied"

But this isn't working. Any idea why not and what I should be doing instead?

1

The "Permission denied" from find is output to stderr rather than stdout, and the grep part doesn't affect what is shown in stderr. Here are two ways you can filter them out:

Redirecting stderr to /dev/null

find /media -name "somenamehere" 2>/dev/null

Redirecting stderr to stdout

find /media -name "somenamehere" 2>&1 | grep -v "Permission denied"

Additional resources for input-output redirection

| improve this answer | |
  • THANK YOU!!! Makes total sense, but I never would have known that on my own. #1 above worked beautifully and seems the easier of the two, but am I right to think that #2 might be safer in case there's some OTHER error (not "Permission denied") that might be important to see? Can you think of any examples? I'm usually just trying to see if a file (like a photo on a CF card) has been copied to backup directories yet. But might there be times when these errors are useful? Again, THANK YOU! – confused Jan 9 '16 at 18:54
  • @confused: You could alternatively save stderr errors to a file for later review with 2>stderr.log instead of 2>/dev/null. You could then open stderr.log and see what you might have missed. – Deltik Jan 9 '16 at 18:59

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.