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.

Replies are listed 'Best First'.
RE: DiskUsage Calculator
by merlyn (Sage) on Jul 30, 2000 at 19:41 UTC
    If the guts of ProcessFileSystem were to be rewritten to use File::Find, then you wouldn't go off into ga-ga land once you hit a symlink, and the program would be portable to Macs and Windows too.

    Please don't rewrite File::Find. If you need to recurse through directories, use the built-in tools!

    -- Randal L. Schwartz, Perl hacker

RE: DiskUsage Calculator
by Anonymous Monk on Jul 31, 2000 at 08:11 UTC
    I made a few mods to your script (BTW, very nice). I am using File::Recurse, got rid of a loop (sortedhash), and did some cool stuff with the labels. Hope this helps.
    #!/usr/bin/perl -w # # This program will outline the top users of a file system. # use strict; use Getopt::Long; use File::Recurse; my $max_users=10; my @fs=(); my @file_list=(); my @dir_list=(); my %filehash; GetOptions( "fs=s@" => \@fs, "users=i" => \$max_users ); &help() unless @fs; foreach my $fs (@fs) { my %sorthash; undef(%filehash); print "$fs:\n"; recurse ( sub { my ($uid, $size) = (stat)[4, 7]; $filehash{$uid} += $size; }, $fs); my $count = 0; foreach my $uid ( sort {$filehash{$b} <=> $filehash{$a} } keys %fi +lehash) { next if ($uid eq ""); last unless $count < $max_users; my $name = (getpwuid($uid)) || "uid($uid)"; &printUser($name, $filehash{$uid}); $count++; } print "\n"; } sub printUser { my ( $user, $size ) = @_; my $label; my @labels = qw ( bytes Kb Mb Gb ); foreach my $power (reverse 0..$#labels){ if($size >= (1024**$power)){ $label = $labels[$power]; $size /= (1024**$power); last; } } printf " %-12s is using %7.2f %s\n", $user, $size, $label; } sub help() { print "Syntax:\n\t$0 [--users=i] --fs=/path1 [--fs=/path2] ...\n"; exit(0); }
RE: DiskUsage Calculator
by Anonymous Monk on Jul 31, 2000 at 08:10 UTC
    I made a few mods to your script (BTW, very nice). I am using File::Recurse, got rid of a loop (sortedhash), and did some cool stuff with the labels. Hope this helps.
    #!/usr/bin/perl -w # # This program will outline the top users of a file system. # use strict; use Getopt::Long; use File::Recurse; my $max_users=10; my @fs=(); my @file_list=(); my @dir_list=(); my %filehash; GetOptions( "fs=s@" => \@fs, "users=i" => \$max_users ); &help() unless @fs; foreach my $fs (@fs) { my %sorthash; undef(%filehash); print "$fs:\n"; recurse ( sub { my ($uid, $size) = (stat)[4, 7]; $filehash{$uid} += $size; }, $fs); my $count = 0; foreach my $uid ( sort {$filehash{$b} <=> $filehash{$a} } keys %fi +lehash) { next if ($uid eq ""); last unless $count < $max_users; my $name = (getpwuid($uid)) || "uid($uid)"; &printUser($name, $filehash{$uid}); $count++; } print "\n"; } sub printUser { my ( $user, $size ) = @_; my $label; my @labels = qw ( bytes Kb Mb Gb ); foreach my $power (reverse 0..$#labels){ if($size >= (1024**$power)){ $label = $labels[$power]; $size /= (1024**$power); last; } } printf " %-12s is using %7.2f %s\n", $user, $size, $label; } sub help() { print "Syntax:\n\t$0 [--users=i] --fs=/path1 [--fs=/path2] ...\n"; exit(0); }
RE: DiskUsage Calculator
by Strahinja (Initiate) on Jul 30, 2000 at 18:20 UTC

      Now, I know this will sound a little heretic, but I think I saw Bash script that did the disk usage counting, but, apparently, it didn't took care for separate users.

      Anyway, your script rocks, it is pretty handy for all those newbie-sysadmins out there.

    Strahinja
      *AHEM* du -c *AHEM*