How to use 'grep -v' on 'du' output in Linux?

351 Views Asked by At

I'm running below command:

du -hs * | sort -hr | grep -v "*directory*"

But, I getting output as below:

[myuser@ip-1-2-3-4 opt]$ du -hs * | sort -hr | grep -v "*cannot read directory*"
du: cannot read directory ‘XYZ’: Permission denied
du: cannot read directory ‘ABC/DEF’: Permission denied
80G dir1
27G dir2
15G dir3

I want to exclude lines: cannot read directory.

Anything am I missing here ?

1

There are 1 best solutions below

0
Bencho Naut On BEST ANSWER

The "cannot read" lines are on STDERR, so a normal pipe would not process them further.

To filter it:

du -hs * 2>&1 | sort -hr | grep -v 'cannot read directory'

P.S.: (Also it is enough to match on the "cannot read directory string")