I'm trying to write a perl script to report the size of the subdirectories of a certian higher dir. I looked on perlmonks.org and from what I could gather -s $foo will report the size of a file, but I can't find any documentation on this. Like does this report bytes or kilobytes? Either way, it does not look to be right. Here is the code that I have so far, it is a reworking of some code I found on here to do a similar job.
#!/usr/bin/perl
use strict;
my $dir = '.';
my %directories = ();
my $dirsRef = \%directories;
&find_size($dir, $dirsRef);
foreach my $key (sort keys %directories) {
print "$key has $directories{$key} bytes in it\n";
}
sub find_size {
my ($currentDir, $dirsRef) = @_;
opendir DIR, $currentDir;
my @files = grep !/^\.\.?$/, readdir DIR;
for my $blah (@files) {
if (-d $blah) {
&find_size($blah, $dirsRef);
} else {
$dirsRef->{$currentDir} += (-s $blah);
}
}
}
Am I on the right track? Any suggestions are greatly appreciated.
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.