Size of all files in a directory?

B

Bill H

There is probably a perl command for this but I can't seem to find it
(and knowing how my questions have been answered in the FAQ posting
recently it will probably show up in a few minutes). But here goes, is
there a command for getting the size of all the files in a directory
(total size)?

Bill H
 
P

Paul Lalli

There is probably a perl command for this but I can't seem to find it
(and knowing how my questions have been answered in the FAQ posting
recently it will probably show up in a few minutes). But here goes, is
there a command for getting the size of all the files in a directory
(total size)?

No, but it's not exactly difficult to do in a couple commands:

use List::Util qw/sum/;
open my $dh, $dir or die "Cannot open directory $dir: $!":
my $total_size = sum( map { -f "$dir/$_" ? -s _ : 0 } }
readdir($dh));

Written out more explicitly:
open my $dh, $dir or die "Cannot open directory $dir: $!":
my $total_size = 0;
while (my $file = readdir($dh)) {
next if !-f "$dir/$file";
$total_size += -s _;
}

Paul Lalli
 
G

Gunnar Hjalmarsson

Paul said:
No, but it's not exactly difficult to do in a couple commands:

use List::Util qw/sum/;
open my $dh, $dir or die "Cannot open directory $dir: $!":

Suppose you mean

opendir ...
 
B

Brian Helterlilne

Paul said:
No, but it's not exactly difficult to do in a couple commands:

use List::Util qw/sum/;
open my $dh, $dir or die "Cannot open directory $dir: $!":
opendir

my $total_size = sum( map { -f "$dir/$_" ? -s _ : 0 } }
readdir($dh));

Written out more explicitly:
open my $dh, $dir or die "Cannot open directory $dir: $!":
opendir

my $total_size = 0;
while (my $file = readdir($dh)) {
next if !-f "$dir/$file";
$total_size += -s _;
}
 
P

Paul Lalli

Suppose you mean

opendir ...

Damnit. That's the one bug I make all the time in my real programs
too, and am constantly confused as to why I'm not finding any files in
the directory. Usually I catch it after 30 seconds...

I really wish readdir() returned an error if it was passed a
filehandle reference rather than a directoryhandle reference...

Thanks for the correction, Gunnar & Brian.

Paul Lalli
 
G

Gunnar Hjalmarsson

Paul said:
I really wish readdir() returned an error if it was passed a
filehandle reference rather than a directoryhandle reference...

$! seems to contain what you want.

$ cat test.pl
open( my $dh, '.' );
my @files = readdir $dh or die $!;
$ perl test.pl
Inappropriate ioctl for device at test.pl line 2.
$
 
P

Paul Lalli

$! seems to contain what you want.

$ cat test.pl
open( my $dh, '.' );
my @files = readdir $dh or die $!;
$ perl test.pl
Inappropriate ioctl for device at test.pl line 2.

Yes, but if you're doing it the "right" way:

open my $dh, '.';
while (my $file = readdir $dh) {
print "$file\n";
}

then you're not really given any reason to know that an operating
system error occurred. It just looks like no files were read. That
is, there's nothing intuitively telling me readdir returned false
because there was an OS error, or because the directory actually was
empty. I guess I would rather it throw an exception.

Paul Lalli
 
C

comp.llang.perl.moderated

Yes, but if you're doing it the "right" way:

open my $dh, '.';
while (my $file = readdir $dh) {
print "$file\n";

}

then you're not really given any reason to know that an operating
system error occurred. It just looks like no files were read. That
is, there's nothing intuitively telling me readdir returned false
because there was an OS error, or because the directory actually was
empty. I guess I would rather it throw an exception.

You could get something very similar with Fatal's "succeed or die"
semantics:

use Fatal qw/open readdir/;

eval {open ...; readdir ..; ... };
die $@ if $@;

which generates:

Can't readdir(GLOB(0x23780)): Bad file number
at ...
 
P

Paul Lalli

You could get something very similar with Fatal's "succeed or die"
semantics:

Interesting, I hadn't considered that. Thanks for the idea. Of
course, that still requires me to remember to use Fatal any time I use
readdir, and if I remember the reason I'm using Fatal, I'm likely to
remember the mistake I keep making with open/opendir. :)

Paul Lalli
 
P

Paul Lalli

Interesting, I hadn't considered that. Thanks for the idea.

Uhm, I conditionally take that back. ;-) Fatal seems to severely
FUBAR readdir(). What am I doing wrong?

$ ls temp/
foo.1 foo.2 foo.3
$ perl -wle'
use Fatal qw/readdir/;
opendir my $dh, "temp" or die $!;
my @files = readdir($dh);
print for @files;
'
..
$ perl -wle'
#use Fatal qw/readdir/;
opendir my $dh, "temp" or die $!;
my @files = readdir($dh);
print for @files;
'
..
...
foo.1
foo.2
foo.3

It looks like when Fatal is use, readdir always acts like it's being
called in scalar context. That cannot be a good thing. . .

Paul Lalli
 
J

John W. Krahn

Paul said:
Uhm, I conditionally take that back. ;-) Fatal seems to severely
FUBAR readdir(). What am I doing wrong?

$ perl -wle'
use Fatal qw/readdir/;
opendir DH, "." or die $!;
my @files = readdir DH;
print for @files;
'
Name "main::DH" used only once: possible typo at -e line 3.
test

It appears that the directory handle is invalidated at some point?


John
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,770
Messages
2,569,583
Members
45,075
Latest member
MakersCBDBloodSupport

Latest Threads

Top