Industry guru Dave Taylor offers tech support on technical and business topics, including iPhone, iPod, Microsoft Windows, Sony PSP, cellphones, online advertising, CSS, Web design, business, Unix, Linux, SEO, Mac OS X, and shell script programming.     


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!


Related Shell Script Programming articles:
✔   Secretly capture screenshots on my Mac?
When I used to work on a Linux system, there was a utility we had that would let me take screen captures every...
✔   Parsing "id" strings in a Shell Script?
Hello Dave. I need a Bash shell script that creates a directories with the group names automatically when user logs in to the...
✔   Copy and Paste from the Mac OS X Command Line?
I am constantly running commands in Terminal.app on my MacBook and then copying and pasting the results into email messages or documents. Yes,...
✔   Script to test line lengths for Twitter compatibility?
I've been tasked with writing a series of tweets for a Black Friday marketing campaign and am finding it a bit tricky because...
✔   Shell script to convert lowercase to title case?
As part of a project I'm working on, I find myself deep in a Linux shell script, needing to have a subroutine that...

Let's stay in touch!
Sign up for my weekly AskDaveTaylor Newsletter and you'll receive even more tech and gadget help right to your inbox, along with exclusive news and industry updates. It's good stuff. I promise!
    Enter your name: and your email addr:  









Reader Comments To Date: 6

Subramanyam said, on November 13, 2006 11:39 PM:

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

Deanypop said, on November 28, 2006 5:48 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.

imran said, on February 22, 2007 8:35 AM:

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

Dave Taylor said, on February 27, 2007 11:33 PM:

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

vinod parmar said, on April 16, 2008 12:14 AM:

how to find disk usage of a particular directory?

Antriksh Pany said, on March 20, 2010 11:49 PM:

Disk usage for a 'directory':

du -s directory
du -sh directory # More human readable
du -sh # If in the directory already

Starbucks coffee cup I do have a lot to say, and questions of my own for that matter, but first I'd like to say thank you, Dave, for all your helpful information by buying you a cup of coffee!

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











I will never send you any unsolicited email. Ever.






Check This Out Too...

 
Look for Answers
Need Help? Ask Dave Taylor!


Follow Me on Pinterest

Find Me on Google+
ADT on G+
© 2002 - 2013 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. Further, 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. My lawyer says "Thanks".
"Ask Dave Taylor®" is a registered trademark of Intuitive Systems, LLC.