30

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?

Anthon
81.4k42 gold badges174 silver badges228 bronze badges
asked May 12, 2014 at 0:42

4 Answers 4

62

That's nothing to do with grep - it's because the pipe | redirects the standard output stream stdout whereas the Permission denied messages are in the standard error stream stderr. You could achieve the result you want by combining the streams using 2>&1 (redirect the stream whose file descriptor is 2 to the stream whose file descriptor is 1) so that stderr as well as stdout gets piped to the input of the grep command

find / -name libGL.so.1 2>&1 | grep -v 'denied'

but it would be more usual to simply discard stderr altogether by redirecting it to /dev/null

find / -name libGL.so.1 2>/dev/null

Using |& instead of 2>&1 |

If you take a look at the Bash man page you'll likely notice this blurb:

If |& is used, the standard error of command is connected to command2's standard input through the pipe; it is shorthand for 2>&1 |.

So you can also use this construct as well if you want to join STDERR and STDOUT:

find / -name libGL.so.1 |& grep -v 'denied'
slm
379k126 gold badges793 silver badges897 bronze badges
answered May 12, 2014 at 1:01
1
  • 3
    Additional note on combining them: If for some bizarre reason the file exists at /foo/bar/denied/libGL.so.1, combining stdout and stderr then filtering would hide the result Commented May 12, 2014 at 18:17
5

Your command should be:

find / -name libGL.so.1 2>/dev/null

Find is complaining about permissions on standard error (fd2). In order to eliminate those lines, redirect (>) standard out to the bit bucket (/dev/null).

answered May 12, 2014 at 1:00
4

The "permission denied" lines are going to the stderr (standard error) stream, but you are piping stdout (standard out) through grep.

You can redirect away stderr entirely with

find / -name libGL.so.1 2> /dev/null
answered May 12, 2014 at 1:01
2

Have you tried calling the command with sudo?

sudo find / -name libGL.so.1

If it still shows the message, use the already mentioned redirect of stderr (fd=2) to nirvana (/dev/null):

sudo find / -name libGL.so.1 2> /dev/null

More ideas here, good luck!

answered May 12, 2014 at 2:04

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.