Beefy Boxes and Bandwidth Generously Provided by pair Networks
P is for Practical
 
PerlMonks  

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

by Aighearach (Initiate)
on Jun 27, 2001 at 13:39 UTC ( [id://91871]=note: print w/replies, xml ) Need Help??


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

perl -e '@sorted = sort { -M $a <=> -M $b } </path/*>; unlink foreach +@sorted[10..$#sorted]'

--
Snazzy tagline here

Replies are listed 'Best First'.
Re: Re: find "x" quantity newest files in a dir
by merlyn (Sage) on Jun 27, 2001 at 18:10 UTC
    You might find this rather expensive if you have 100 files or so, but if the directory is constantly being trimmed to 10 or 15 files, that's probably OK. Just in case someone copies your code for another task, it's nice to know how it scales.

    -- Randal L. Schwartz, Perl hacker

Re: Re: find "x" quantity newest files in a dir
by runrig (Abbot) on Jun 27, 2001 at 18:39 UTC
    unlink accepts a list, so you might say:
    unlink @sorted[10..$#sorted] if @sorted > 10;

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others learning in the Monastery: (2)
As of 2024-04-25 03:36 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found