in reply to Re: sorting a directory
in thread sorting a directory

heres something i came up with. please dont laugh at me cos I am really new to all this. my objective is to get 100 files from a directory having the oldest access times and then delete them(something like a cache)
sub make_space { my $folder_name = $_[0]; opendir DIR_HANDLE, ".$folder_name"; my @filelist = readdir DIR_HANDLE; close DIR_HANDLE; foreach $filename(@filelist) { $st = stat($ilename) $actime = st->$atime } #custom sort routine to sort the filelist according to their last acce +ssed date @sorted_list = sort { ($actime{$a}) > ($actime{$b}) } @filelist; #deleting 1st 100 entries in the sorted array for($i =0; $i <=99 ;$i=$i+1) { unlink($sorted_list[i]); } }

Replies are listed 'Best First'.
Re: thanx a lot guys
by superfrink (Curate) on Dec 27, 2004 at 10:23 UTC
    Oh well if you want to delete the 100 oldest files in a directory and you are using a unix-like system them the following might work for you.
    ls -1rt /home/frink/code/perl/z/ | head -100 | xargs rm