Exclude all permission denied messages from "du"

LinuxShellWarningsSuppress WarningsDu

Linux Problem Overview


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”.

https://stackoverflow.com/q/762348

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

https://stackoverflow.com/q/5463884

Linux Solutions


Solution 1 - Linux

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 

Solution 2 - Linux

2> /dev/nul hides only error messages.

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

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

Solution 3 - Linux

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 

Solution 4 - Linux

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

du -sh 2>&1 | grep -v  '^du:'

Solution 5 - Linux

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 )

Solution 6 - Linux

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.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionWen_CSEView Question on Stackoverflow
Solution 1 - LinuxMevatlaveKraspekView Answer on Stackoverflow
Solution 2 - LinuxCristian TView Answer on Stackoverflow
Solution 3 - LinuxClaire TView Answer on Stackoverflow
Solution 4 - LinuxKZiovasView Answer on Stackoverflow
Solution 5 - Linux18446744073709551615View Answer on Stackoverflow
Solution 6 - LinuxPrathyusha SandilyaView Answer on Stackoverflow