This is a little script I wrote to while working as a lab supervisor. The script examines one or more directories, and displays how much space each user in using in that directory and all of it's subdirectories. I wrote this so that I could quickly find out which users were wasting the most space in dirs like /var/mail, /samba/profiles, /tmp and such. It came in quite handy on several occasions, so I am sharing it with you.

!/usr/bin/perl # # This program will outline the top users of a file system. # use strict; use Getopt::Long; my $max_users=10; my @fs=(); my @file_list=(); my @dir_list=(); my %filehash; GetOptions( "fs=s@" => \@fs, "users=i" => \$max_users ); &help() if not @fs; foreach my $fs (@fs) { my %sorthash; my $i=0; undef(%filehash); print "$fs:\n"; &processFileSystem($fs); foreach my $uid (keys %filehash) { next if ($uid eq ""); my $name = (getpwuid($uid)) || "uid($uid)"; push @{$sorthash{$filehash{$uid}}}, $name; } foreach my $size (reverse sort numerically keys %sorthash) { last if not ($i < $max_users); foreach my $user ( @{ $sorthash{$size} } ) { &printUser($user,$size); $i++; } } print "\n"; } sub processFileSystem( ) { my $dir = shift @_; my @readdir_list; opendir "currdir", $dir; @readdir_list = readdir "currdir"; close "currdir"; foreach my $curr (@readdir_list) { next if $curr =~ /^\.{1,2}$/; if ( -d "$dir/$curr" ) { &processFileSystem("$dir/$curr"); } else { my ( @stat ) = stat "$dir/$curr"; $filehash{$stat[4]} += $stat[7]; } } } sub printUser() { my ( $user, $size ) = @_; my $label; if ( $size >= ( 1024 * 1024 * 1024 ) ) { $size /= ( 1024 * 1024 * 1024 ); $label = "Gb"; } elsif ( $size >= ( 1024 * 1024 ) ) { $size /= ( 1024 * 1024 ); $label = "Mb"; } elsif ( $size >= ( 1024 ) ) { $size /= ( 1024 ); $label = "Kb"; } else { $label = "bytes"; } printf " %-12s is using %7.2f %s\n", $user, $size, $label; } sub numerically() {$a <=> $b} sub help() { print "Syntax:\n\t$0 [--users=i] --fs=/path1 [--fs=/path2] ...\n"; exit(0); }


I hope that you all enjoy it, lemme know what you think.

In reply to DiskUsage Calculator by qi3ber

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.