DEV Community

mich0w0h
mich0w0h

Posted on

Linux find: remove error lines from outputs

What I want to achieve

  • find a specific file which I have read permission for
  • ignore error lines of "Permission denied"

What didn't work

Firstly I tried this command to remove the "Permission denied" lines.

$ find /path/ -readable | grep -v "denied"
Enter fullscreen mode Exit fullscreen mode

However, it didn't work.

That's because the error messages are on a separate stream from the grep targets.

The "Permission denied" messages are printed to the standard error stream (stderr), while the grep command only filters the standard output stream (stdout).

How to tackle

  1. Redirect both output streams using 2>&1 which redirects stderr(file descriptor 2) to stdout(file descriptor 1).
$ find /path/ -readable 2>&1 | grep -v "denied"
Enter fullscreen mode Exit fullscreen mode
  1. suppress stderr putting away into /dev/null/, a special file that discards any data written to it, effectively silencing stderr.
$ find /path/ -readable 2>/dev/null
Enter fullscreen mode Exit fullscreen mode

Top comments (0)