in reply to recursive directory question

use File::Find; my %ages; # $ages{$dirname}{old}, $ages{$dirname}{new} find sub { return unless -f; $ages{$File::Find::dir}{-M _ > 180 ? 'old' : 'new'}++; }, "."; # put your topdirs here for (sort keys %ages) { if ($ages{$_}{old} > $ages{$_}{new}) { print "$_ has more old than new\n"; } }

-- Randal L. Schwartz, Perl hacker


update: Hey, that was actually useful! I found three directories I could archive! Neat! Thanks for the idea. I'll put it into a mini-snippets article for one of my upcoming column articles!

Replies are listed 'Best First'.
Re: *Re: recursive directory question
by Fletch (Bishop) on Jul 08, 2002 at 11:34 UTC

    Trivial improvement: stick my $top = shift || "."; before the find and change the "." to $top and it's even more useful.

Re^2: recursive directory question
by flounder99 (Friar) on Jul 08, 2002 at 12:09 UTC
    I have a question - What does the -M _ do? I understand that the -M returns the modification time (since script start) but what is the _ ? I see this in the File::Find man page but I don't have a good grip on what is going on. Does it mean $_ ? If so why not use $_ ?

    Anyway, nice solution.

    --

    flounder

      The underscore filehandle (one of the few features of Perl to which I can claim to have introduced), means "don't actually perform a stat, but use the information cached from the most recent other stat". Well, the docs in perlfunc say it better:
      If any of the file tests (or either the "stat" or "lstat" operators) are given the special filehan- dle consisting of a solitary underline, then the stat structure of the previous file test (or stat operator) is used, saving a system call. (This doesn't work with "-t", and you need to remember that lstat() and "-l" will leave values in the stat structure for the symbolic link, not the real file.) (Also, if the stat buffer was filled by a "lstat" call, "-T" and "-B" will reset it with the results of "stat _"). Example: print "Can do.\n" if -r $a || -w _ || -x _; stat($filename); print "Readable\n" if -r _; print "Writable\n" if -w _; print "Executable\n" if -x _; print "Setuid\n" if -u _; print "Setgid\n" if -g _; print "Sticky\n" if -k _; print "Text\n" if -T _; print "Binary\n" if -B _;

      -- Randal L. Schwartz, Perl hacker

        Doh! I guess I was too lazy and impatient to RTFM that far. I just glanced at -M and quit reading.

        --

        flounder