Beefy Boxes and Bandwidth Generously Provided by pair Networks
We don't bite newbies here... much
 
PerlMonks  

Filesize counting recursive

by FiReWaLL (Scribe)
on Jan 21, 2000 at 23:32 UTC ( [id://2313]=perlquestion: print w/replies, xml ) Need Help??

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

$directory = '/thedir/thatiwantto/count'; print "\n\n"; print CountSize($directory); print "\n\n"; sub CountSize { my ($directory) = $_[0]; my ($ask, $filesize, $item, $size, @raiz); @raiz = glob("$directory/*.*") || warn(); foreach $item (@raiz) { $ask = -d $item; if ($ask) { $size += &CountSize($item); } else { $size += -s $item; } } return($size); }
It should return the total size of all the tree. In Windows, it works, but in Linux, it doesn't. It returns only the current directory file sizes. What should I do?

Replies are listed 'Best First'.
Re: Filesize counting recursive
by Crulx (Monk) on Jan 22, 2000 at 01:27 UTC
    1. Please use <code> around your code blocks. *grin*
    2. Glob calls the shell to do its expansion. Thus you will not get a cross platform way of searching for files with it. command.com's "all files" is *.* In unix, that would mean getting all the files with a . in their name which could be less than the total number of files.
    A unix version of the code you had before would be
    use strict; my $directory = '.'; print "\n\n"; print CountSize($directory); print "\n\n"; sub CountSize { my $directory = shift; $directory .= "/*"; my ($ask, $filesize,$item, $size, @raiz); @raiz = glob($directory); foreach $item (@raiz) { $ask = -d $item; if ($ask) { $size += &CountSize($item); } else { $size += -s $item; } } return($size); }
    However, this is not the "right way" to do this. You should be using opendir and readdir.
    use strict; my $dir = '.'; print &dir_tree_size($dir) . "\n"; exit 0; sub dir_tree_size { my $dir = shift; my ($i,$total); $total = 0; opendir DIR, $dir; my @files = grep !/^\.\.?$/, readdir DIR; for $i (@files) { if(-d $i) { $total += dir_tree_size($dir . "/$i") } else { $total += -s $i} } return $total; }
    Hope this helps.
    Crulx
Re: Filesize counting recursive
by stephen (Priest) on Jan 22, 2000 at 14:32 UTC
    Another way to do this would be to use the File::Recurse module from CPAN. Like so:
    use strict; use File::Recurse; MAIN: { my $dir = $ARGV[0]; my $size = 0; recurse(\&sum_size, $dir, \$size); print "Total size: $size\n"; } sub sum_size { my ($file, $total_size_ref) = @_; stat($file); unless (-d _) { $$total_size_ref += -s _; } 1; }
    There's something funky about the above... it produced larger values than crulx's code for a couple of my directories, reason unknown. Seemed fairly accurate, though...
      Never heard of it. I cannot find it on CPAN either. Is is part of another File:: package?

      Crulx

        (Note: perl -MCPAN -e shell is WONDERFUL from the Unix command line)

        Here's the data I found on it:
        Module id = File::Recurse
        CPAN_USERID ASHER (Aaron Sherman <ajs@ajs.com>)
        CPAN_VERSION 1.0
        CPAN_FILE ASHER/File-Tools-2.0.tar.gz
        INST_FILE (not installed)

Re: Filesize counting recursive
by FiReWaLL (Scribe) on Jan 22, 2000 at 21:56 UTC
    Thanks! It worked! I'll use code next time too *g*.
RE: Filesize counting recursive
by Anonymous Monk on Jan 26, 2000 at 07:46 UTC
    You probably don't even want to do this. The program du (disk usage) should be on your *nix box, and does this already, just call it from your program. See man du for more info.
Re: Filesize counting recursive
by dlc (Acolyte) on Jan 25, 2000 at 00:38 UTC

    i think you're going about this all wrong. what's wrong with du -s from the shell prompt? why use perl when a much simpler and faster command already exists? if you absolutely need to get this into perl, try my $dirsize = `du -s`.

      du works, but its not cross-platform. As you may have noted in the original question, he's developing on an NT box and deploying on *nix... vroom hit the spot, me thinks! :o)
Re: Filesize counting recursive
by FiReWaLL (Scribe) on Jan 26, 2000 at 19:19 UTC
    I don't have access to the whole machine. And I don't want to know all the usage of the HD, I just want to know the DU of that directory (my website)
      Well du -s directory should do the job for counting usage for everything within a given directory including all the subdirectories.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://2313]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others sharing their wisdom with the Monastery: (6)
As of 2024-04-23 08:36 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found