I'm developing a script to monitor and take action against users who use to much disk space. At this point I'm simply collecting the information. Here is the code:

#!/usr/bin/perl use strict; use warnings; # This script will the top ten space users # of any directory # Neil Watson Mon Mar 1 13:05:11 EST 2004 use Getopt::Std; use File::Find; use Data::Dumper; my (%opt, $utotal, $uid, $uname, $fname, %size, $size, %files); my ($count); # options available # -d <dir to check> getopts("d:", \%opt); my $dir = $opt{d}; # get file file information find(\&wanted, $dir); # get top ten offenders print "User\t\tTotal (kb)\n"; foreach $uname (sort keys %size){ print $uname."\t\t".$size{$uname}."\n"; } #print Dumper(%files); sub wanted{ # scan only regular files if (-f){ # owner of file but skip # if owner is not a user # (uid < 500) $uid = (lstat($_))[4]; unless ($uid < 500){ $uname = getpwuid $uid; # gather name of file $fname = $File::Find::name; # size of file (kb) $size = (lstat($_))[7]; $size = int($size/1000); # store in a hoh # username {filename} => filesize $files{$uname} {$fname} = $size; # keep running total of each # user's files $size{$uname} += $size; } } }

Now that I look at my hoh data structure I'm not sure that is it ideal. For instance, how would I select the top ten space users? I think there must be a more efficient way to do this so I'm throwing this out to my fellow monks.

Neil Watson
watson-wilson.ca

In reply to Monitoring disk usage by neilwatson

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.