-1

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 ?

Cyrus
  • 84,225
  • 14
  • 89
  • 153
Temp Expt
  • 305
  • 1
  • 4
  • 17
  • 2
    Replace `du -hs *` with `du -hs * 2>/dev/null`? – Cyrus Mar 14 '23 at 06:33
  • Are you sure you don't want those error lines? If, by any chance, the disk space loss is inside one of those directories, you won't know about it anymore. – Dominique Mar 14 '23 at 07:45
  • `*` at the beginning of a regex is interpreted as a literal asterisk. You are probably looking for `.*` which is the regular expression for "any character, repeated zero or more times, as many as possible" but you don't need a wildcard here; `grep` looks for a match anywhere in the line. – tripleee Mar 14 '23 at 08:29
  • Does this answer your question? [What are the differences between glob-style patterns and regular expressions?](https://stackoverflow.com/questions/23702202/what-are-the-differences-between-glob-style-patterns-and-regular-expressions) – tripleee Mar 14 '23 at 08:29
  • Also https://stackoverflow.com/questions/31901741/how-do-i-redirect-errors-to-dev-null-in-bash – tripleee Mar 14 '23 at 08:30

1 Answers1

0

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")

Bencho Naut
  • 348
  • 1
  • 6