!/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); }