I started using sed recently. One handy way I use it is to ignore unimportant lines of a log file:

tail -f example.com-access.log | sed '/;/ELB-/d;/408 0 "-" "-"/d;'

But when I try to use it similarly with find, the results aren't as expected. I am trying to ignore any line that contains "Permission denied" like this:

find . -name "openssl" | sed '/Permission denied/d;'

However, I still get a whole bunch of "Permission denied" messages in stdout.


As mentioned in the correct answer below, the "Permission denied" messages are appearing in stderr and NOT stdout.

  • You should look into grep/egrep. – Michael Hampton Jul 10 '13 at 2:13
  • @MichaelHampton I don't see how grep would help me out here. – Ben Harold Jul 10 '13 at 2:14
  • By filtering things out of the log file that you don't want to see, of course. – Michael Hampton Jul 10 '13 at 2:17
  • 1
    @Ben You can, however, exclude the results that you do know. (-v) – Andrew B Jul 10 '13 at 2:53
  • 2
    Last comment, because this is about to get locked. If this works, upvote with no reply. find . -name "openssl" 2>&1 | grep -v "Permission denied" – Andrew B Jul 10 '13 at 4:35

The problem is error ouput printed to stderr, so the sed command can't catch the input. The simple solution is: redirecting stderr to stdout.

find . -name "openssl" 2>&1 | sed '/Permission denied/d;'
  • Beat me to it. The subshell isn't necessary though. – Andrew B Jul 10 '13 at 2:58
  • So the error is printed to stderr whereas sed is reading from stdout? Also, could you explain "2>&1" specifically? – Ben Harold Jul 10 '13 at 3:18
  • @Ben Output redirection only effects STDOUT. 2>&1 redirects file descriptor 2 (STDERR) to file descriptor 1 (STDOUT). – Andrew B Jul 10 '13 at 3:45
  • @Andrew: edited answer. And I think 2>1 is the same as 2>&1. – cuonglm Jul 10 '13 at 4:55
  • 3
    @Gnouc: "2>1" redirects stdout (file descriptor 2) to a file named "1". It isn't the same as "2>&1", where the "&1" indicates "file descriptor 1, aka stdout". – ThatGraemeGuy Jul 10 '13 at 7:57

I'm not sure why you are trying to use sed to remove permission denied messages from output of find - unless you are trying to learn how to use sed.

I would simply run this instead:

find . -name "openssl" 2>/dev/null

Here, I'm redirecting stderr (file descriptor 2) over to /dev/null (refer to man null). In other words, 2>/dev/null simply discards everything written to stderr.

  • Great stuff. You wouldn't believe how many verbose and needlessly complex answers I've seen for this... – Robbie Dee Nov 2 '16 at 9:46

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