On a mailing list I’m on, someone asked an interesting question about the du command: “I want to figure out how much disk space I’m using in different directories, but I don’t know how to exclude certain directories from the output. Anyone have a suggestion?”
My initial response was that you’d want to just pour the output through a grep -v, or, if you want to get fancy and use a regular expression, grep -vE. Like this:
du * | grep -vE ‘(/usr/|/bin/|/mnt/)’
which would give you an exhaustive output of disk usage by directory, but would skip any directories that matched the pattern /usr/ or /bin/ or /mnt/
It answered the question, but the intent of the person, however, was to also speed up the disk usage calculation itself; what he really wanted was to mask the directories within the du command. As he asked: “Would that skip retrieving the disk usage for any of those
directories, or simply skip displaying the disk usage? The main problem
with retrieving the information on those directories is that it can take a
lot time.”
It doesn’t skip getting the information, you’re right. If you want to do
that, then you need to explicitly list the directories you DO want to know
about. One way to do that, though, is to drop a list of all directories in
the current location into a file, then edit it to be what you want.
Something like this:
find . -mindepth 1 -maxdepth 1 -type d -print > .localdirs
then edit the results to match your needs. Whenever you want to run the
du, use:
$ du -s $(cat .localdirs)
Or, if you’re a backtick sort of person:
$ du -s `cat .localdirs`
I hope that helps you solve your problem!
How Do I Skip Certain Directories In “du” Output?
On a mailing list I’m on, someone asked an interesting question about the du command: “I want to figure out how much disk space I’m using in different directories, but I don’t know how to exclude certain directories from the…
Great data! Thanks for the update. Even better, the -I flag is a mask, so you can have a simple pattern (well, sequence of characters) that will mask more than one directory. However, that’s a potential problem too, because -I admin would also block a folder called MyAdminTools and similar…
Add a “-I foldername” (that’s minus-capital-i) to the du command for each folder to skip:
brighthold:~ % du -s admin
320760 admin
brighthold:~ % du -s admin/tcsh
448 admin/tcsh
brighthold:~ % du -s -I tcsh admin
320312 admin
Notice that the result of the last du (320,312) is the size of admin (320,760) minus the size of tcsh (448).
Here are similar examples omitting a second directory:
brighthold:~ % du -s admin/serverconfigs
8 admin/serverconfigs
brighthold:~ % du -s -I serverconfigs admin
320752 admin
brighthold:~ % du -s -I serverconfigs -I tcsh admin
320304 admin
brighthold:~ %
HTH
-Dave Frank