Industry guru Dave Taylor answers free tech support questions about a wide variety of business and technical topics, including blogging, Google AdSense, MySpace, Sony PSP, Apple iPod, Mp3 players, management, Linux, SEO, Mac OS X, Facebook, Twitter, LinkedIn and Microsoft Windows.

How do I create a group-based disk usage script?

A reader writes:
"I've been reading your book Teach Yourself Unix in 24 Hours and learning a lot, but I'm stuck on a disk usage problem. Suppose there are 5 people (A, B, C, D, E) in our group and they share the same directory called "group." The total capacity of this directory is 100 GB. I'm looking for a program/command/script that can be run to generate the following space usage update:
 
    A    5         5%
    B   10        10%
    C   20        20%
    D   40        40%
    E    5         5%
 total  80        80%
  
   free space     20%
This is an interesting question, so let's dig into it and see what we can figure out...

Dave's Answer: First off, you say that the users are sharing a particular directory, so I'm going to assume you mean that they all have the same home directory on the system, but have discrete entries in the password file (that is, different user IDs). This would be shown if you did an ls -l and saw files owned by each of the users in the same home directory.

That being the case, I'll first suggest that you put them into different home directories. You could even have those directories under the shared directory, as in /home/group/A and /home/group/B, etc. That'd make things easy to track. Failing that, it's still quite possible to ascertain disk usage on a per userID basis by using the find command.

For example, consider using find / -user A -type f -print, which would generate a list of all files owned by user A across the entire file system, or, better, using the slightly more complex find / -user A -type f -exec ls -ld \{\} \; which generates output similar to:

-rw-------  1 taylor  staff   21508  1 Nov 23:20 ./.DS_Store
-rw-r--r--  1 taylor  staff       0  8 Apr  2004 ./.localized
-rwxr-xr-x  1 taylor  staff  950272 29 May  2003 ./021022_0801.bin
-rwxr-xr-x  1 taylor  staff      69 17 Sep  2003 ./021022_0801.cue

With that information, you should be able to create a brief awk script that sums up all the numbers in column #5, divides by 1024 and presents the final disk space used by each user in the group in Kb (or divide by 1024*1024 and get Mb). To go from that to calculating what % of the total alloted space for the group each person is using should be straightforward.

Hope that helps you!



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

Subscribe!

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

Comments

Hi Dave,

I want the unix shell script to check the disk usage at each mount point and if the space is less than 2GB(at any mount point) it should send a mail to my email id. In this, the values will be in GB or MB sometimes, so I want the script to exactly compare the value 2GB.

Hoping for your answer soon,

With Regards & thanks
Subramanyam Karanam

Posted by: Subramanyam at November 13, 2006 11:39 PM

I would love to see a version of this script, and for that matter the disk monitoring script from Dave's "wicked cool scripts" book, that doesn't use /etc/passwd, but rather copes with a multitude of LDAP users.

Posted by: Deanypop at November 28, 2006 5:48 PM

I am putting this command on unix but not getting the result - find / -user A -type f -print

Posted by: imran at February 22, 2007 8:35 AM

As always, Imran, check the man page for your version of Unix to ensure that the flags work as you expect. For example, -user might require a user ID number, not name...

Posted by: Dave Taylor at February 27, 2007 11:33 PM

how to find disk usage of a particular directory?

Posted by: vinod parmar at April 16, 2008 12:14 AM

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.









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


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 - 2008 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]