Beefy Boxes and Bandwidth Generously Provided by pair Networks
Pathologically Eclectic Rubbish Lister
 
PerlMonks  

Re: find "x" quantity newest files in a dir

by grinder (Bishop)
on Jun 27, 2001 at 13:04 UTC ( [id://91864]=note: print w/replies, xml ) Need Help??


in reply to find "x" quantity newest files in a dir

Use the file names as hash keys, and assign the age of the file to the hash value. Then sort the hash by value, and skip over the first ten. The rest you can blow away or otherwise do as you please. Something like this should get you started:

#! /usr/bin/perl -w use strict; my $directory = shift || '.'; opendir D, $directory or die "Cannot open directory $directory: $!\n"; my %age; while( defined( my $file = readdir D )) { next if $file eq '.' or $file eq '..'; my $canonical = "$directory/$file"; $age{$canonical} = (stat $canonical)[9]; } closedir D; my $count = 0; foreach my $file ( sort {$age{$b} <=> $age{$a}} keys %age ) { next if ++$count < 10; print "$file (@{[scalar localtime $age{$file}]})\n"; # or unlink $file if you are feeling brave }

update: Oops! I had the equality operator around the wrong way. As things stood, this code would have unlinked the ten most recent files and kept the rest! It's now around the right way. (Tip o' the hat to tomhukins). Also changed the code to reflect the sane comments of tachyon below.


--
g r i n d e r

Replies are listed 'Best First'.
Re: Re: find "x" quantity newest files in a dir
by tachyon (Chancellor) on Jun 27, 2001 at 16:46 UTC

    Nice post grinder ++ A couple of problems with your code though. First you need to specify the full path to the files for stat - outside of the ./ dir your code returns the dreaded 1970 date for everything. Second you use $count = 0; next if $count++ > 10 which saves the first 11 files as $count is 0 to start (File 1), 10 at file 11 (still not > 10) and 11 at file 12. You need either >=10 or ++$count. No doubt this relates to the reversal of the inequality. I have supplied some reassuring test code below which lists the files with date and shows what will get unlinked. You also need the full path for the unlink.

    cheers

    tachyon

    use strict; my $directory = 'c:/windows'; opendir D, $directory or die "Cannot open directory $directory: $!\n"; my %age; while( my $file = readdir D ) { next if $file eq '.' or $file eq '..'; $age{"$directory/$file"} = (stat "$directory/$file")[9]; } closedir D; my $count = 0; foreach my $file ( sort {$age{$b} <=> $age{$a}} keys %age ) { if ($count++ < 10) { print "Newest: $file (@{[scalar localtime $age{$file}]})\n"; } else { print "For Delete: $file (@{[scalar localtime $age{$file}]})\n +"; # unlink $file; # uncomment at your own risk! } }

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others avoiding work at the Monastery: (2)
As of 2024-04-26 04:11 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found