Róbert Nagy
Róbert Nagy

Reputation: 7642

grep command invert option not working

I'm trying to filter out the lines, that contain the Permission denied from the find command.

If I'm running: find ~ -name "fifo" | grep "Permission denied"

The output is correct: find: ‘/home/nroby/.cache/dconf’: Permission denied find: ‘/home/nroby/.dbus’: Permission denied find: ‘/home/nroby/.config/enchant’: Permission denied

However if I'm using the -v option: find ~ -name "fifo" | grep -v "Permission denied"

The output is: find: ‘/home/nroby/.cache/dconf’: Permission denied find: ‘/home/nroby/.dbus’: Permission denied /home/nroby/Desktop/Linux/fifo find: ‘/home/nroby/.config/enchant’: Permission denied

Upvotes: 3

Views: 1863

Answers (1)

user2371524
user2371524

Reputation:

The lines saying Permission denied come from stderr, not stdout. The terminal displays both streams, but the pipe redirects only stdout (so with your command, grep never sees a line containing Permission denied).

So, if you only want to silence these messages, do this instead:

find ~ -name "fifo" 2>/dev/null

2> redirects stderr.

If you need to pipe both stdout and stderr for some reason (e.g., you want other error messages present in your output), you can do this:

find ~ -name "fifo" 2>&1 | grep -v "Permission denied"

2>&1 redirects stderr to stdout.

Upvotes: 11

Related Questions