Finding disk space used by Windows or Unix directory

J

john_ramsden

For Windows and Unix I need a perl module or script that
returns the disk space occupied by a specified directory
(including all files and subdirectories recursively).

I could write one using File::Find or equivalent; but I'd
rather not reinvent the wheel if there's already a tried
and tested module or code out there, which having spent
some fruitless time searching I'm beginning to doubt.


Cheers

John R Ramsden

P.S. Filesys::DiskSpace is no good for what I need, as it
only returns the space available & used on an entire file
system.
 
A

Anno Siegel

For Windows and Unix I need a perl module or script that
returns the disk space occupied by a specified directory
(including all files and subdirectories recursively).

I could write one using File::Find or equivalent; but I'd
rather not reinvent the wheel if there's already a tried
and tested module or code out there, which having spent
some fruitless time searching I'm beginning to doubt.


The problem with disk space measurement is not so much gathering the data
(easy with File::Find), but the interpretation. Under Unix there are hard
and soft links to consider. There is no way of treating them right under
all circumstances. This particular wheel needs the re-invention of one
or two spokes every time.

Anno
 
J

Jürgen Exner

For Windows and Unix I need a perl module or script that
returns the disk space occupied by a specified directory
(including all files and subdirectories recursively).

I could write one using File::Find or equivalent; but I'd
rather not reinvent the wheel if there's already a tried
and tested module or code out there, which having spent
some fruitless time searching I'm beginning to doubt.


Oh come on, that's a five-liner:

use File::Find;
my $total;
sub wanted {$total += -s;}
find (\&wanted, 'c:/tmp');
print $total;

jue
 
J

John W. Krahn

Anno said:
For Windows and Unix I need a perl module or script that
returns the disk space occupied by a specified directory
(including all files and subdirectories recursively).

I could write one using File::Find or equivalent; but I'd
rather not reinvent the wheel if there's already a tried
and tested module or code out there, which having spent
some fruitless time searching I'm beginning to doubt.


The problem with disk space measurement is not so much gathering the data
(easy with File::Find), but the interpretation. Under Unix there are hard
and soft links to consider. There is no way of treating them right under
all circumstances. This particular wheel needs the re-invention of one
or two spokes every time.


And don't forget sparse files which are applicable under NTFS as well as most
Unix and Linux file systems. :)


John
 
J

john_ramsden

Jürgen Exner said:
For Windows and Unix I need a perl module or script that
returns the disk space occupied by a specified directory
(including all files and subdirectories recursively).

I could write one using File::Find or equivalent; but I'd
rather not reinvent the wheel if there's already a tried
and tested module or code out there, which having spent
some fruitless time searching I'm beginning to doubt.


Oh come on, that's a five-liner:

use File::Find;
my $total;
sub wanted {$total += -s;}
find (\&wanted, 'c:/tmp');
print $total;


Ah OK, that's easier than I thought. Just one footnote though - When I
incorporate this in a module function (using "strict"
and "warnings" as always) it pops up a curious message,
where line 702 was in the _next_ function:

Variable "$total" will not stay shared at WD/Misc.pm line 702.

To eliminate this irritating and baffling message, I had to change "my
$total" to "use vars qw { $total }". God knows why;
it must be some subtle scope issue.

Anyway, thanks for the replies everyone.
 
J

Joe Smith

Jürgen Exner said:
Oh come on, that's a five-liner:

use File::Find;
my $total;
sub wanted {$total += -s;}
find (\&wanted, 'c:/tmp');
print $total;

That does not return the total disk usage.
You're assuming that a one-byte file takes up just one
byte on the disk, when it really takes up an entire
block (or an entire cluster).

use constant BLOCK => 4096; # May be different on each disk
sub wanted {$total += int ((BLOCK - 1 + -s) / BLOCK) * BLOCK; }


-Joe
 
J

Jürgen Exner

Joe said:
That does not return the total disk usage.
You're assuming that a one-byte file takes up just one
byte on the disk, when it really takes up an entire
block (or an entire cluster).

That's true. My suggestion compiles the total file size (by some definition
of file size).
As Anno has pointed out there are many more pitholes in _what_ you want to
calculate when getting into sparse files, special files, hardlinked files,
softlinks, etc.

jue
 
G

Gunnar Hjalmarsson

For Windows and Unix I need a perl module or script that
returns the disk space occupied by a specified directory
(including all files and subdirectories recursively).

I could write one using File::Find or equivalent; but I'd
rather not reinvent the wheel if there's already a tried
and tested module or code out there, which having spent
some fruitless time searching I'm beginning to doubt.


Recently I wrote a CGI script for monitoring disk usage on Linux. I used
the du command, and possibly that's available on Unix as well (but
probably not on Windows).

Anyway, it may give you some ideas:

http://gunnar.cc/cgi-bin/cvsweb.cgi/misc/diskusage.cgi?cvsroot=gunnar
 
R

Rasto Levrinc

Joe said:
use constant BLOCK => 4096; # May be different on each disk
sub wanted {$total += int ((BLOCK - 1 + -s) / BLOCK) * BLOCK; }

Why not to use block size and blocks values from stat function?

perldoc -f stat
 

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

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top