I have the following command:
find / -name libGL.so.1
Which returns lots of lines with "Permission denied". I want to exclude such lines, so I added the following:
find / -name libGL.so.1 | grep -v 'denied'
But the output is the same – my grep -v 'denied'
is not filtering out the lines with Permission denied
. I've tried many variations, looked over grep tutorials, but I cannot figure out the problem. Any suggestions?
Best Answer
That's nothing to do with
grep
- it's because the pipe|
redirects the standard output streamstdout
whereas thePermission denied
messages are in the standard error streamstderr
. You could achieve the result you want by combining the streams using2>&1
(redirect the stream whose file descriptor is2
to the stream whose file descriptor is1
) so thatstderr
as well asstdout
gets piped to the input of the grep commandbut it would be more usual to simply discard
stderr
altogether by redirecting it to/dev/null
Using |& instead of 2>&1 |
If you take a look at the Bash man page you'll likely notice this blurb:
So you can also use this construct as well if you want to join STDERR and STDOUT: