Beefy Boxes and Bandwidth Generously Provided by pair Networks
No such thing as a small change
 
PerlMonks  

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

by thatguy (Parson)
on Jun 28, 2001 at 00:59 UTC ( [id://92050]=note: print w/replies, xml ) Need Help??


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

I saw this node this morning and I thought right off that find(1) did something like this natively.. it does catch the files last accessed or created within a time frame and execute a command, but I couldn't figure out how to delete everything that was older than the 10 newest. (for the curious it's: find ./ -type f -atime +7 -exec rm {} \;)

but hey, since you can tack commands into find..
find ./ -type d -exec perl -e '$limit="11";$dir="{}";$i=0;@list=`ls -ltF $dir`;print "Directory: $dir\n";foreach(@list) {chomp;$ls="$_";if (($ls=~ m/\//o) ne 1) {if (($i < $limit) && (($ls=~ m/total/o) ne 1) && (($ls=~ m/@/o) ne 1)) {print "keep $ls\n";} elsif ($i > ($limit-1)) {print "rm $ls\n";};} else {$i=($i-1);};$i++;};' {} \;
ooogly, eh? and yeah, it's set up for testing.. no actual rm'ing going on.. but that's easy enough to fix.
this was my fun activity for today.

Replies are listed 'Best First'.
Re: find "x" quantity newest files in a dir
by Abigail (Deacon) on Jun 28, 2001 at 03:02 UTC
    Or just:
    find . -type f | perl -nle 'print -M, "\t$_"' | sort -n | \ + tail +10 | cut -f 2 | xargs rm
    This assumes there are no filenames with newlines in them.

    -- Abigail

      hmm.. well, dang. mine started out so small!

      I am only now begining to see the fine art of perl and I think it's going to be a long time before I get away from the bludgeoning I do now.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others cooling their heels in the Monastery: (3)
As of 2024-04-16 15:27 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found