Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer
 
PerlMonks  

How do I delete files based on their timestamp?

by Anonymous Monk
on May 08, 2000 at 20:47 UTC ( #10629=perlquestion: print w/replies, xml ) Need Help??

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; }

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others examining the Monastery: (9)
As of 2021-04-21 10:36 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found

    Notices?