I am trying to write a simple perl script that will iterate through the regular files in a directory and calculate the total size of all the files put together. However, I a
If it isn't just your typo -s _
instead of the correct -s $_
then please remember that readdir
returns file names relative to the directory you've opened with opendir
. The proper way would be something like
my $base_dir = '/path/to/somewhere';
opendir DH, $base_dir or die;
while ($_ = readdir DH) {
print "size of $_: " . (-s "$base_dir/$_") . "\n";
}
closedir DH;
You could also take a look at the core module IO::Dir which offers a tie
way of accessing both the file names and the attributes in a simpler manner.
You have a typo:
$cursize = -s _;
Should be:
$cursize = -s $_;