#!/usr/bin/perl -w # $Id: piglist 468 2006-12-11 01:57:35Z hsinclai $ # harold@hastek.net use strict; use Getopt::Std; use File::Find; use Cwd; getopts('h'); our($opt_h); my $workdir; my $wide_term = 0; my @dirlist; my %sizelist; my $marker = '-' x 56; my $markerlong = $marker x 2; my $me = $0; $me =~ s@^\.\/|\/.*\/@@g; my $version = '0.1'; &usage if defined $opt_h; #---Get directory for analysis if ( $ARGV[0] ) { $workdir = $ARGV[0] ; -d $workdir || &bad_directory; } else { $workdir = getcwd(); } #---Get the dirs and sizes finddepth( { wanted => \&dirlister }, $workdir ); for my $entryin ( @dirlist ) { my $dusize = qx!/usr/bin/du -s "$entryin"!; my $realsize = (split /\t/,$dusize)[0]; chomp $realsize; $sizelist{$entryin} = $realsize; #to see real du output #system("/usr/bin/du", "-s", "$entryin"); } #---Path names longer than about 44 will need a wider display foreach ( keys(%sizelist) ) { length($_) > 44 and $wide_term = 1; } #---Print #------------------Header print "\n\n Sizes in K for $workdir\n"; $wide_term == 0 ? print ' ' . $marker . $/ : print ' ' . $markerlong . $/ ; #------------------Content my @ordered = sort { $sizelist{$b} <=> $sizelist{$a} } keys %sizelist; if ( $wide_term == 0 ) { print sprintf("%15d ",$sizelist{$_}), sprintf("%-65s",$_) . $/ for @ordered; } elsif ( $wide_term == 1 ) { print sprintf("%15d ",$sizelist{$_}), sprintf("%-132s",$_) . $/ for @ordered; } #------------------Footer print $/; $wide_term == 0 ? print ' ' . $marker . $/ : print ' ' . $markerlong . $/; print $wide_term == 0 ? sprintf("% 57s","$me v$version") : sprintf("% 113s","$me version $version") ; print $/x2; #---Subs sub dirlister { return if -f $_; return if $_ =~ /^\.\.$/; -d $_ and push @dirlist, $File::Find::name; return @dirlist; } sub usage { print "\n Usage: $me [PATH] \n"; print ' ' . $marker . $/; print " List individual size in K for subdirectories of PATH\n"; print " If no path specified, use current working directory \n\n"; exit 0; } sub bad_directory { print "\n Error: Cannot find directory \"$workdir\" \n\n"; exit 1; } =head1 NAME piglist - report directory sizes =head1 SYNOPSIS piglist [PATH] piglist -h If PATH is specified, piglist reports subdirs of PATH, and If PATH is omitted, the current directory is listed. piglist -h invokes usage. =head1 DESCRIPTION piglist makes a formatted, sorted list of subdirectory names and their sizes in K. If the path names are longer than 44 characters, piglist reformats itself to output a 132 character wide report. piglist uses File::Find and the output of 'du', on unix/linux =head1 WARNINGS Recursive File::Find/du can be a little expensive, and might be impolite to run on a busy or shared machine. piglist does not follow symbolic links, maybe should add this ability =head1 BUGS AND CAVEATS To get a proper listing in wide format, make the terminal at least 150 characters wide; piglist uses a few more characters for margins. Because of line wrapping, output in an 80x24 terminal looks bad. =cut #eof