Vadmeggy
Vadmeggy

Reputation: 123

How can use invert grep after pipe?

I want to list directory disk usage in a fileserver. I also want to ignore the error messages. Here is my command:

du -sh * | grep -v "Permission denied" | sort -n

The result still contains the permission denied lines:

du: cannot access './myFile1/': Permission denied
du: cannot access './myFile2/': Permission denied
du: cannot access './myFile3/': Permission denied

What am I doing wrong?

Upvotes: 3

Views: 1011

Answers (1)

fedorqui
fedorqui

Reputation: 289815

This is because the "Permission denied" is sent through standard error, not through standard output.

If you don't want this information, just silence it by redirecting stderr to /dev/null:

du -sh * 2>/dev/null | sort -n

This happens with all these error messages:

$ touch a
$ ls a asfasd
ls: cannot access asfasd: No such file or directory
a
$ ls a asfasd | grep cannot
ls: cannot access asfasd: No such file or directory
$ ls a asfasd 2>/dev/null  
a

Upvotes: 6

Related Questions