brianarn has asked for the wisdom of the Perl Monks concerning the following question:
I know that if nothing else, I could just do something like:sub clean_dir { my $dir = shift; my %age; my $debug = 1; # Loop through files and add ages into array opendir (DIR, $dir) or die "Unable to open $dir: $!"; while (readdir (DIR)) { my $temp_age = -M "$dir/$_"; if ($debug) { print "Age checked in readdir loop: $temp_age<BR>\n"; print "File checked in readdir loop: $_<BR>\n"; } $age{$temp_age} = $_; } # DEBUG if ($debug) { print "Ages:<BR>\n"; print "$_: $age{$0}<BR>\n" foreach sort keys %age; print "Oldest files...<BR>\n"; } foreach (sort ({keys (%age)}[0..9])) { print "$_[0]<BR>\n" if ($debug); # Commented out removes to be safe for now #system("rm", "$dir/$age{$_[0]}") # or die "Can't remove $dir/$age{$_[0]}: $!"; print "$_[0] removed." if ($debug); } }
but that seems a little cheap - is there a better way to do this?my @files = split /\n/, `ls -rtl $dir`; system ("rm","$dir/$_") for @files[0..9];
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Cleaning up by age
by Jazz (Curate) on Nov 13, 2001 at 00:31 UTC | |
by Jonathan (Curate) on Nov 15, 2001 at 18:21 UTC | |
by FoxtrotUniform (Prior) on Nov 20, 2001 at 22:21 UTC | |
|
Re: Cleaning up by age
by Albannach (Monsignor) on Nov 13, 2001 at 00:54 UTC | |
by brianarn (Chaplain) on Nov 13, 2001 at 01:24 UTC | |
|
Re: Cleaning up by age
by Rich36 (Chaplain) on Nov 13, 2001 at 00:47 UTC |