I was recently searching for a way to filter out 'Permission denied' errors while searching for a file using the find
command, and I found this link:
How can I exclude all "permission denied" messages from "find"?
Here's an the answer that worked for me from the link:
find . -name "filename" 2>&1 | grep -v 'permission denied'
Although this answer works, I don't fully understand why we need the 2>&1
(redirection of stderr to stdout).
Doesn't the output from the command find . -name "filename" | grep -v 'Permission denied'
already redirect to stdout?
Because the
'permission denied'
message is printed in stderr not stdout.1
is stdout2
is stderr&
specifies that whatever following is a file descriptor not filename2>&1
redirectsstderr
tostdout
and enables the error message to be piped into thegrep
command.If excluding
permission denied
message is all you need, then this will do it without usinggrep
: