c has asked for the wisdom of the Perl Monks concerning the following question:

I am familiar with using stat to get the size of a file on a unix machine. However, when i attempt the same call on a directory, I am not getting what I would hope for. That being the sum of the file sizes of that directory's contents. In unix speak, a du -sk for that directory. My current code is:

$info = (stat( "$dir ))[7];

Where $dir has a proper value, such as /home. Do I actually need to read through a directory and its subdirs adding up file sizes as I go?

humbly -c

Replies are listed 'Best First'.
Re: diskspace used by a directory
by jepri (Parson) on Jul 25, 2001 at 07:32 UTC
    Yup.

    I don't know the ins and outs of ext2, but I do know that each directory is really an index of files. The size you are seeing is the size of the index. It's always a multiple of your cluster size.

    ____________________
    Jeremy
    I didn't believe in evil until I dated it.

Re: diskspace used by a directory
by jlongino (Parson) on Jul 25, 2001 at 07:36 UTC
    Are you trying to do this in Windows or in Unix? If Unix:
    $dirsize = `du -ks /home`; $dirsize = (split " ", $dirsize)[0]; print "$dirsize\n\n";
    On a Windows system of course it is a different matter.

    Update: Thanks John for fleshing out the Windows angle.

      Yes, I certainly could, but I was curious to know whether or not there was a perl module that I could use to do this, or another way without calling a shell function. If not, then this is probably the way I'll go.

      humbly -c

        Something worth considering:
        Tie         GBARR     Tie-Dir-1.02.tar.gz               2k  26 Apr 1996
        from CPAN would save you some steps. But I haven't actually used the module myself. Here's the internal docs:
        This module provides a method of reading directories using a hash. The keys of the hash are the directory entries and the values are a reference to an array which holds the result of C<stat> being called on the entry. The access and modification times of an entry can be changed by assign +ing to an element of the hash. If a single number is assigned then the acc +ess and modification times will both be set to the same value, alternative +ly the access and modification times may be set separetly by passing a reference to an array with 2 entries, the first being the access time and the second being the modification time. . . . . SYNOPSIS tie %hash, Tie::Dir, "."; new Tie::Dir \%hash, "."; $hash = new Tie::Dir "."; # itterate through the directory foreach $file ( keys %hash ) { ... } # Set the access and modification times (touch :-) $hash{SomeFile} = time; # Obtain stat information of a file @stat = @{$hash{SomeFile}}; # Check if entry exists if(exists $hash{SomeFile}) { ... } # Delete an entry delete $hash{SomeFile};
        I would be cautious when testing it out since it appears you could modify times/dates or even delete files with it.

        Good luck.
      On a Windows system, `dir /u $dirname` gives something like:
      Volume in drive F is Work Serial number is 244F:5AF9 Directory of F:\dev\experiment\ScanWord\* 366,148 bytes in 5 files and 2 dirs 393,216 bytes allocated 4,040,884,224 bytes free
      which can be parsed back in.

      That's what I get using the 4NT 4.00A command shell; with COMMAND.COM and CMD.EXE results and actual flags to dir may be different.

      In one application, I needed to know how much room a set of files would take up, before actually copying them to the target media (A CD-R). The filesystem of the staging area has different properties, so I couldn't just ask the filesystem, but had to compute it myself. Since I was already copying files over, it was easy to accumulate the size, rounding up to the sector size of the target.

      —John

Re: diskspace used by a directory
by $code or die (Deacon) on Jul 25, 2001 at 17:39 UTC
    Yep. Check out this node and its responses. There are also a couple of other nodes that super search might reveal.

    Good luck

    Error: Keyboard not attached. Press F1 to continue.
Re: diskspace used by a directory
by meonkeys (Chaplain) on Jul 25, 2001 at 08:01 UTC
    If you were to create a module or add a method to a module to do this, any idea in what namespace it would belong?
      Certainly under the File:: branch. Adding to an existing module, check out File::stat.