77

I am trying to evaluate the disk usage of a number of Unix user accounts. Simply, I am using the following command:

du -cBM --max-depth=1 | sort -n

But I’ve seen many error message like below. How can I exclude all such “Permission denied” messages from display?

du: `./james/.gnome2': Permission denied

My request could be very similar to the following list, by replacing “find” to “du”.

How can I exclude all "permission denied" messages from "find"?

The following thread does not work. I guess I am using bash.

Excluding hidden files from du command output with --exclude, grep -v or sed

KZiovas
  • 3,491
  • 3
  • 26
  • 47
Wen_CSE
  • 1,225
  • 2
  • 10
  • 8

6 Answers6

113
du -cBM --max-depth=1 2>/dev/null | sort -n 

or better in bash (just filter out this particular error, not all like last snippet)

du -cBM --max-depth=1 2> >(grep -v 'Permission denied') | sort -n 
MevatlaveKraspek
  • 2,246
  • 3
  • 20
  • 23
  • @MevatlaveKraspek - thank you! if you have time, could you explain what "2> >(grep -v 'Permission denied')" does? – Sergej Fomin Aug 06 '18 at 08:46
  • `-v` is an inverse match. So seems like it just filters `stderr` (2>) and only hides `Permission denied` errors instead of hiding all errors like in the first command. – huysentruitw Aug 13 '18 at 08:10
  • can stack the greps like du -cBM --max-depth=1 2> >(grep -v 'Permission denied') | grep -v 'cannot access' | sort -n, it is also possible to add multiple terms to grep with an OR – twobob Oct 13 '22 at 00:07
  • if one day stackoverflow will go down, we all go back to the stone age – HAL9000 Nov 09 '22 at 11:49
11

To remove all errors coming from the du command i used this:

du -sh 2>&1 | grep -v  '^du:'
KZiovas
  • 3,491
  • 3
  • 26
  • 47
10

2> /dev/null hides only error messages.

the command du always try run over directory. Imagine that you have thousands of directories?

du needs eval, if you have persmission run if not, follow with the next dir...

Community
  • 1
  • 1
Cristian T
  • 109
  • 2
8

I'd use something concise that excludes only the lines you don't want to see. Redirect stderr to stdout, and grep to exclude all "denied"s:

du -cBM --max-depth=1 2>&1 | grep -v 'denied' | sort -n 
Claire T
  • 81
  • 1
  • 1
2

If 2>/dev/null does not work, probably the shell you are using is not bash.

To check what shell you are using, you may try ps -p $$ (see https://askubuntu.com/a/590903/130162 )

18446744073709551615
  • 16,368
  • 4
  • 94
  • 127
2

you can pipe it to a temporary file, like -

du ... > temp_file

Errors get printed on the terminal and only disk usage information gets printed into the temp_file.