Industry guru Dave Taylor offers free tech support on a wide variety of technical and business topics, including HTML, online advertising, Cascading Style Sheets, Web design, management, Unix, Linux, search engine optimization, online dating, Mac OS X, shell script programming and Microsoft Windows.

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 output. Anyone have a suggestion?"

Dave's Answer:

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!


Help others find this article at Del.icio.us, Digg, Netscape, Reddit, and Stumble Upon    

Subscribe!

Never miss another useful Q&A article again! Subscribe to AskDaveTaylor with Google Reader.

Comments

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

Posted by: David E. Frank at December 2, 2004 9:28 PM

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

Posted by: Dave Taylor at December 2, 2004 9:35 PM

I have a lot to say, but ...
Starbucks coffee cup I have a lot to say, and questions of my own for that matter, but most of all I'd like to say thank you for all your efforts on this Web site by buying you a chai!

I do have a comment, now that you mention it!









Remember personal info?


Please note that I will never send you any unsolicited commercial email. Ever.

While I'm at it, please note that by submitting a question or comment you're agreeing to my terms of service, which are: you relinquish any subsequent rights of ownership to your material by submitting it on this site.









Uniblue: Free Virus Scan

Search
Find just the answers you seek from among our 2000+ free tech support articles by using our Lijit search engine.


Linux Journal: Free Issue!

Help!





Subscribe to
Ask Dave Taylor!

Add to Google Reader
Add to My Yahoo!
Subscribe in NewsGator Online

RDF   XML

Free Updates!
Sign up and get free weekly updates and special offers on books, seminars, workshops and more.


Recent Entries
Join the List!
Join my author info mailing list, where you'll learn about my upcoming books, speaking gigs, and more!


Book Links
© 2002 - 2009 by Dave Taylor. All Rights Reserved.

Note: This web site is for the purpose of disseminating information for educational purposes, free of charge, for the benefit of all visitors. We take great care to provide quality information. However, we do not guarantee, and accept no legal liability whatsoever arising from or connected to, the accuracy, reliability, currency or completeness of any material contained on this web site or on any linked site.

[whiteboard marker tray]
"Ask Dave Taylor®" is a registered trademark of Intuitive Systems, LLC.