Delete Files older than 3 hrs

Hi ! I want to go through all *.scf files in a directory, check if they're older (creation date) than 3 hrs and if so delete them. How can I do this with Perl ? Hint: Its enough to check if actual time is bigger than filetime+3 hrs

regards
ItsMe
ItsMeAsked:
Who is Participating?

[Webinar] Streamline your web hosting managementRegister Today

x
 
thoellriConnect With a Mentor Commented:
How bout this?
  Tobias

Change the last line to a real "unlink" to actually delete the files.

#!/usr/local/bin/perl

use strict;
my($dir)="/a/directory";
opendir(D,$dir) || die "cannot open dir $dir - $!";
my($threeHrsAgo)=time-3*60*60;
my(@filesToDelete)=grep /\.scf$/ && (stat("$dir/$_"))[9] < $threeHrsAgo, readdir(D);
close(D);
print "unlink($_);\n" foreach(@filesToDelete);
0
 
thoellriCommented:
Beware that the above script uses the "last modification time" as opposed to the "creation time" of the file.

Tobias
0
 
mr_whoCommented:
Hello,

Another option is to do this:
$boo = `find *.scf  -amin 300 | xargs rm -rf`;

Where 300 is minutes, en watch the backticks.

good luck!

tha fox!
0
 
ItsMeAuthor Commented:
thanks !
0
All Courses

From novice to tech pro — start learning today.