Exclude all permission denied messages from “du”

2019-02-02 02:59发布

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

4条回答
再贱就再见
2楼-- · 2019-02-02 03:21
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 
查看更多
仙女界的扛把子
3楼-- · 2019-02-02 03:34

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 
查看更多
等我变得足够好
4楼-- · 2019-02-02 03:36

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 )

查看更多
疯言疯语
5楼-- · 2019-02-02 03:42

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

查看更多
登录 后发表回答