On 06.06.2010 18:12, newbie01 perl wrote:
Hi all,
Oracle core dumps are created as directories. I want to remove these
directories after a certain period of time, for example 10 days.
I have the following script that searches a directory for files that are
older than 10 days and possibly remove them. At the moment, for the sake of
testing, am printing them out for the meantime.
While the code below seems to work, it also include the main directory as
well in the deletion, can someone advise how I can exclude that from the
deletion?
Hello there,
IMO you have to exclude '.' and '..' directories from the unlink loop.
Try something like this:
my $dir = '/raid/test';
opendir(DIR,$dir) || die "Can't open $dir : $!\n";
my @files = readdir(DIR);
close(DIR);
chdir $dir || die "$!\n";
foreach my $file(@files)
{
#print "$dir/$file\n";
if ($file !~ /\.+/)
{
my $now = time;
my @stat = stat("$dir/$file");
if ($stat[9] < ($now -1))
{
print "Deleting $dir/$file...";
#unlink("$dir/$file");
print "Done.\n";
}
}
}
.. and the output is:
Deleting /raid/test/remove...Done.
Deleting /raid/test/remove2...Done.
--
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/