#!/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",$_) . $/ f
+or @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 PA
+TH\n";
print " If no path specified, use current working director
+y \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 th
+eir 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 impo
+lite to run on
a busy or shared machine.
piglist does not follow symbolic links, maybe should add this abilit
+y
=head1 BUGS AND CAVEATS
To get a proper listing in wide format, make the terminal at least 1
+50 characters wide;
piglist uses a few more characters for margins.
Because of line wrapping, output in an 80x24 terminal looks bad.
=cut
#eof
In reply to piglist
by hsinclai
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: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.