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…
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!
Disk usage for a ‘directory’:
du -s directory
du -sh directory # More human readable
du -sh # If in the directory already
how to find disk usage of a particular directory?
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…
I am putting this command on unix but not getting the result – find / -user A -type f -print
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.
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