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

Is there an equivalent to a find -mtime or -ctime in a Find module? I didn't seem to find one. I know I could use Find and then turn around and do stat on each file, but I don't know how much overhead that would cause. The reason I'm trying to move past using the UNIX/Linux equivalents is the list limits getting hit (this is for a deleting/cleanup tool). I know I can tinker with regular UNIX/Linux find to increase the default limits, but I think the built in Perl hash/array should be able to hold more.
  • Comment on Find -mtime or -ctime from a Find module?

Replies are listed 'Best First'.
Re: Find -mtime or -ctime from a Find module?
by MidLifeXis (Monsignor) on Sep 17, 2015 at 13:35 UTC

    You could use the -X or stat calls in your wanted function.

    --MidLifeXis

Re: Find -mtime or -ctime from a Find module?
by Corion (Patriarch) on Sep 17, 2015 at 13:37 UTC
Re: Find -mtime or -ctime from a Find module?
by Laurent_R (Canon) on Sep 17, 2015 at 13:41 UTC
    There are modules, such as the File::Find module, that can do just that for you, all you need to do is to pass it the relevant callback subroutine that will check the age of each file using stat or one the -M -A -C file test operators.
Re: Find -mtime or -ctime from a Find module?
by RichardK (Parson) on Sep 17, 2015 at 14:41 UTC

    Your shell problem sounds like you should be using xargs. something along these lines :-

    find /tmp -name core -type f -print | xargs /bin/rm -f
      find /tmp -name core -type f -print | xargs /bin/rm -f

      To avoid nasty surprises, change that to:

      find /tmp -name core -type f -print0 | xargs -0 /bin/rm -f

      This way, find uses a null char instead of a line feed to seperate filenames, and xargs expects a null char instead of any whitespace between filenames. As the null char is the only char that can not be part of a filename, this is the only way to reliably separate filenames.

      Alexander

      --
      Today I will gladly share my knowledge and experience, for there are no sweeter words than "I told you so". ;-)
Re: Find -mtime or -ctime from a Find module? ( File::Find::Rule qw/ find rule :Age /;
by Anonymous Monk on Sep 17, 2015 at 23:32 UTC

    Re: File test in grep not excluding current directory ( use File::Find::Rule qw/ find rule :Age /; ) , -X,

    #!/usr/bin/perl -- use strict; use warnings; use Path::Tiny qw/ path /; use File::Find::Rule qw/ find rule :Age /; path( q{goner/goner/goner/newy.txt} )->touchpath->spew(1234); path( q{goner/goner/goner/oldy.txt} )->touchpath->touch( time - 60*60* +60 ); find( file => age => [ newer => '1D' ], exec => sub { print "#<1D# $_[2]\n"; return !!0; }, in => "goner", ); my $printer = sub { ## my( $shortname, $path, $fullname ) = @_; if(-M _ > 1 ){ print "#>1D# $_[2]\n"; } return !!0; ## means discard }; rule( file => size => '<6', exec => $printer, )->in( "goner" ); path( "goner" )->remove_tree; __END__ #<1D# goner/goner/goner/goner.txt #>1D# goner/goner/goner/oldy.txt