Linux terminal commands: showing non-match

Written by

in

Sometimes you want to weed out the “cannots.”

The following is as I recall it.

Yesterday I was in a server file system using the Linux terminal. I wanted to know about the folders the terminal could access, not the ones it couldn’t. Yet, there were more it couldn’t.

I pipelined the command into grep:

command | grep -v “cannot”

but it was still showing the “cannot” lines.

I learned from another source that the reason is grep only works on standard output, file handle 1 in the terminal, whereas “cannot” typically will come from standard error, file handle 2.

When I tried

command 2>&1 | grep -v cannot

I got what I was hoping for. Apparently 2>&1 feeds standard error into standard output, so grep can work on it.

Interesting, eh?

Source:

stackoverflow.com

man7.org

-JS

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *