Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Exclude all permission denied messages from "du"

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

like image 592
Wen_CSE Avatar asked Feb 28 '13 17:02

Wen_CSE


People also ask

How do I remove permissions denied from find?

How to Exclude All “Permission denied” messages When Using Find Command in UNIX/LINUX? use 2>/dev/null. The 2>/dev/null at the end of the find command tells your shell to redirect the standard error messages to /dev/null, so you won't see them on screen.

Which command will find a file without showing permission denied messages?

When find tries to search a directory or file that you do not have permission to read the message "Permission Denied" will be output to the screen. The 2>/dev/null option sends these messages to /dev/null so that the found files are easily viewed.

What is Permission denied?

While using Linux, you may encounter the error, “permission denied”. This error occurs when the user does not have the privileges to make edits to a file. Root has access to all files and folders and can make any edits. Other users, however, may not be allowed to make such edits.

What does the du command do?

The du command is a standard Linux/Unix command that allows a user to gain disk usage information quickly. It is best applied to specific directories and allows many variations for customizing the output to meet your needs.


3 Answers

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  
like image 50
MevatlaveKraspek Avatar answered Sep 28 '22 03:09

MevatlaveKraspek


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

like image 37
Cristian T Avatar answered Sep 28 '22 03:09

Cristian T


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 
like image 25
Claire T Avatar answered Sep 28 '22 01:09

Claire T