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

Hi,

Maybe I should be writing in a unix forum, but I was hoping this was possible using perl. How can I find directories less than 7 days old? Is CPAN's File::DirList the only way to go? Is there no built in function for this type of search?

Thanks,
Jay

Replies are listed 'Best First'.
Re: Find directories lt 7 days old
by jeffa (Bishop) on Apr 01, 2015 at 16:20 UTC

    This works for me:

    use strict; use warnings; use File::Find::Rule; my @dirs = File::Find::Rule ->directory ->mtime( '>' . (time - (60 * 60 * 24 * 7)) ) ->in( '.' ) ; print "$_\n" for @dirs;

    The tricky part is what to pass to mtime() ... you need to subtract 7 days from the current Epoch (via time) and anything greater than that result falls within the past 7 days. You will need to install File::Find::Rule.

    jeffa

    L-LL-L--L-LL-L--L-LL-L--
    -R--R-RR-R--R-RR-R--R-RR
    B--B--B--B--B--B--B--B--
    H---H---H---H---H---H---
    (the triplet paradiddle with high-hat)
    
      Thank you.
Re: Find directories lt 7 days old
by karlgoethebier (Abbot) on Apr 01, 2015 at 19:27 UTC

    I try to be up-to-date:

    #!/usr/bin/env perl use strict; use warnings; use Path::Iterator::Rule; use feature qw(say); my $amount = ( time - ( 60 * 60 * 24 * 7 ) ); my $rule = Path::Iterator::Rule->new; $rule->mtime(">$amount"); $rule->directory; my @dirs = qw(.); my $next = $rule->iter(@dirs); while ( defined( my $dir = $next->() ) ) { say $dir; } __END__

    Please see Why not recommend IO::All? as well as IO::All and Path::Iterator::Rule for further information.

    Regards, Karl

    «The Crux of the Biscuit is the Apostrophe»

Re: Find directories lt 7 days old
by dave_the_m (Monsignor) on Apr 01, 2015 at 16:59 UTC
    Note that UNIX-like systems do not normally record the time a directory was created, but rather the time it was last modified (-M), last accessed (-A) or its permissions last changed (kinda) (-C).

    Dave.

      Traditionally this was true (the change time being the updating of the inode). Some newer filesystems and tools do support a separate creation time (crtime), change time (ctime), modification time (mtime), and access time (atime). Also, it's worth noting that access time is often disabled or only updated if it's the first access since ctime or mtime updates (via the relatime mount option).

Re: Find directories lt 7 days old
by marinersk (Priest) on Apr 01, 2015 at 15:35 UTC
    Un Now tested, it does in fact select only directories which have been recently updated:
    foreach my $dirnam (@dirnames_I_found) { if ( (-M $dirnam) < 7 ) { print " $dirnam\n"; } }
      Monks really should stop posting untested code ... getting the first reply is not the goal here.

      Your code does not even handle traversing directories or even how to obtain the directories to inspect.

        It has been my experience that use of opendir/closediror File::Findand its ilk does not generally elude a Seeker who quotes the use of a CPAN module in their question.

        I do find, however, that use of -Xfrom perlfuncdoes often elude folks who do not use Perl on a regular basis.

        I provided an example of that built-in functionality which most closely matched what I felt Jeri was seeking.

        I would also note that my then-untested code worked without modification once tested.

Re: Find directories lt 7 days old
by ww (Archbishop) on Apr 01, 2015 at 16:04 UTC

    A reminder that may be needed, despite your years here: Super Search is your friend.

    This is a question asked -- and answered -- many times each year. Perhaps you can find a solution that will satisfy your needs by exerting no more effort than it took to post a lazy question.



    Questions containing the words "doesn't work" (or their moral equivalent) will usually get a downvote from me unless accompanied by:
    1. code
    2. verbatim error and/or warning messages
    3. a coherent explanation of what "doesn't work actually means.
      I don't want to collect all the directories and then analyze them based on their creation date. I wanted it to be more efficient and just collect those I'm interest in. Maybe this is not possible in perl? Also, I'm not lazy.
        Then just set it up to accept an arg that would be a keyword, that way you could atleast filter the dir list.
Re: Find directories lt 7 days old
by Anonymous Monk on Apr 01, 2015 at 16:02 UTC
    File::Find, conditionally append to an array based on a file tests. Nothing outside of core needed.
Re: Find directories lt 7 days old
by KurtSchwind (Chaplain) on Apr 03, 2015 at 15:12 UTC

    Perl is nice. But if you are on a *nix box, just use the find command directly.

    find /path/to/start/dir -type d -ctime -7
    --
    “For the Present is the point at which time touches eternity.” - CS Lewis