2010/6/7 Shlomi Fish <shlo...@iglu.org.il> > On Sunday 06 Jun 2010 19:59:36 Wojciech Łysiak wrote: > > 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 !~ /\.+/) > > Don't do such a check. Use File::Spec->no_upwards() instead. > > Regards, > > Shlomi Fish > > -- > ----------------------------------------------------------------- > Shlomi Fish http://www.shlomifish.org/ > The Case for File Swapping - http://shlom.in/file-swap > > God considered inflicting XSLT as the tenth plague of Egypt, but then > decided against it because he thought it would be too evil. > > Please reply to list if it's a mailing list post - http://shlom.in/reply . > > -- > To unsubscribe, e-mail: beginners-unsubscr...@perl.org > For additional commands, e-mail: beginners-h...@perl.org > http://learn.perl.org/ > > > Hi,
I found a better example at a Perlmonk posting and changed it a bit as below: #!/usr/bin/perl use strict; use File::Find; use File::Spec::Functions; #use File::Spec->no_upwards(); #if ($ARGV[0] eq "") { $ARGV[0]="."; } if ($ARGV[0] eq "") { print "You must specify a directory path ... !!! exiting ... \n"; exit 0; } #...@paths = File::Spec->no_upwards( @paths ); my @file_list; #finddepth ( sub { find ( sub { my $file = $File::Find::name; # - To select files beginning with DATE # if ( -f $file && $file =~ /^DATE_/) { # push (@file_list, $file) # } push (@file_list, $file) }, @ARGV); @file_list = File::Spec->no_upwards( @file_list ); my $now = time(); # get current time my $AGE = 60*60*24*14; # convert 14 days into seconds for my $file (@file_list) { my @stats = stat($file); if ($now-$stats[9] > $AGE) { # file older than 14 days print "$file\n"; } } Sample output run as below: oradev:/oracle/product/10.2.0/scripts/perl>./remove_coredumps.pl/u01/oradump/cdump /u01/oradump/cdump /u01/oradump/cdump/core_7571 /u01/oradump/cdump/core_5705 /u01/oradump/cdump/core_15043 /u01/oradump/cdump/core_25662 /u01/oradump/cdump/core_29081 /u01/oradump/cdump/core_907 /u01/oradump/cdump/core_3853 /u01/oradump/cdump/core_19062 /u01/oradump/cdump/core_11675 /u01/oradump/cdump/core_24237 /u01/oradump/cdump/core_1939 Okay ... looks good so far ... but I still want to exclude the "main" directory which in this case is the one that am giving as the command line argument, is there any way I can exclude it from File::Find ... :(- ... ???