Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > Finding disk space used by Windows or Unix directory

Reply
Thread Tools

Finding disk space used by Windows or Unix directory

 
 
john_ramsden@sagitta-ps.com
Guest
Posts: n/a
 
      02-03-2005

For Windows and Unix I need a perl module or script[s] 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:iskSpace is no good for what I need, as it
only returns the space available & used on an entire file
system.

 
Reply With Quote
 
 
 
 
Anno Siegel
Guest
Posts: n/a
 
      02-03-2005
<(E-Mail Removed)> wrote in comp.lang.perl.misc:
>
> For Windows and Unix I need a perl module or script[s] 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
 
Reply With Quote
 
 
 
 
Jürgen Exner
Guest
Posts: n/a
 
      02-03-2005
http://www.velocityreviews.com/forums/(E-Mail Removed) wrote:
> For Windows and Unix I need a perl module or script[s] 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


 
Reply With Quote
 
John W. Krahn
Guest
Posts: n/a
 
      02-03-2005
Anno Siegel wrote:
> <(E-Mail Removed)> wrote in comp.lang.perl.misc:
>
>>For Windows and Unix I need a perl module or script[s] 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
--
use Perl;
program
fulfillment
 
Reply With Quote
 
john_ramsden@sagitta-ps.com
Guest
Posts: n/a
 
      02-04-2005

Jürgen Exner wrote:
> (E-Mail Removed) wrote:
> > For Windows and Unix I need a perl module or script[s] 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.

 
Reply With Quote
 
Joe Smith
Guest
Posts: n/a
 
      02-04-2005
Jürgen Exner wrote:

> 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
 
Reply With Quote
 
Jürgen Exner
Guest
Posts: n/a
 
      02-04-2005
Joe Smith wrote:
> Jürgen Exner wrote:
>
>> 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).


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



 
Reply With Quote
 
Gunnar Hjalmarsson
Guest
Posts: n/a
 
      02-04-2005
(E-Mail Removed) wrote:
> For Windows and Unix I need a perl module or script[s] 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/...cvsroot=gunnar

--
Gunnar Hjalmarsson
Email: http://www.gunnar.cc/cgi-bin/contact.pl
 
Reply With Quote
 
Rasto Levrinc
Guest
Posts: n/a
 
      02-04-2005
Joe Smith wrote:
>
> 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


--

Rasto Levrinc
http://sourceforge.net/projects/rlocate/
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Getting "not enough disk space error" when there is plenty of disk space Jas Shultz ASP .Net 0 12-03-2003 05:07 PM
Why Python style guide (PEP-8) says 4 space indents instead of 8 space??? 8 space indents ever ok?? Christian Seberino Python 21 10-27-2003 04:20 PM
Re: Why Python style guide (PEP-8) says 4 space indents instead of8 space??? 8 space indents ever ok?? Ian Bicking Python 2 10-24-2003 11:15 AM
Re: Why Python style guide (PEP-8) says 4 space indents instead of8 space??? 8 space indents ever ok?? Ian Bicking Python 2 10-23-2003 07:07 AM
Stack space, global space, heap space Shuo Xiang C Programming 10 07-11-2003 07:30 PM



Advertisments