http://www.perlmonks.org?node_id=10629

Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question: (files)

How do I delete files based on their timestamp?

Originally posted as a Categorized Question.

  • Comment on How do I delete files based on their timestamp?

Replies are listed 'Best First'.
Re: How do I delete files based on their timestamp?
by reptile (Monk) on May 09, 2000 at 00:35 UTC

    Or use perlfunc:stat from within perl. It'll give you just about everything you wanted to know about a file and more. The times returned are in seconds-since-epoch format just like time() so a little arithmetic and a comparison is all you need.

    # delete $file if it's not been modified for 3 hours if ( (stat $file)[9] < time() - (3600 * 3) ) { unlink $file; }
Re: How do I delete files based on their timestamp?
by btrott (Parson) on May 08, 2000 at 21:52 UTC
    Are you on a *nix system? Using find is probably the easiest way:
    find /foo/bar -mtime +7 -exec rm -rf {} \;
    This will find all files under /foo/bar that were modified more than 7 days ago and remove them.

    If you're *nix-impaired :) (and considering that this is a Perl site), use File::Find:

    use File::Find; find(\&wanted, '/foo/bar'); sub wanted { unlink $_ if -M $_ > 7; }