bioskope has asked for the wisdom of the Perl Monks concerning the following question:

hi there I hope the question is in the right format, but can someone guide me as to how to sort a directory according to filesize????? I am so new to perl vut my bosses still want me to complete a work in Perl by Firday. Mind helping me out guys???

Replies are listed 'Best First'.
Re: sorting a directory
by davido (Cardinal) on Dec 22, 2004 at 06:04 UTC

    You can open the directory with opendir, and read it with readdir. push the filenames into an array of arrays (see perllol) along with their filesizes (determined by the -s operator, discussed in perlfunc under the -X heading). And then sort the AoA based on the file sizes using sort. If you want to end up with only the list of filenames (discarding the file sizes), map can be used to transform back to a flat array.


    Dave

Re: sorting a directory
by eyepopslikeamosquito (Archbishop) on Dec 22, 2004 at 06:45 UTC
    In future, before asking a question, I suggest you use the Perl Monks Super Search. This question was asked and answered a while back: Sorting based on filesize.
      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]); } }
        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
Re: sorting a directory
by brian_d_foy (Abbot) on Dec 22, 2004 at 06:13 UTC
Re: sorting a directory
by superfrink (Curate) on Dec 27, 2004 at 09:57 UTC
    Well it's 3:00 AM on Monday for me but I see a recent post so here is some code that ought to give you a lot of ideas. It sorts by atime and I think ought to have enough commented out print statements to help understand what is going on.

    PS: I really recommend the "use strict;" line in there. I put it in every perl program I write. See strict for more info.

    Update: added @lowest_ten_times to show how to get the lowest 10 times.

    #!/usr/bin/perl -w use strict; use Data::Dumper; my $directory = "/home/frink/code/perl/z/"; opendir DIR, $directory; my @file_names = readdir DIR; closedir DIR; # remove "." and ".." @file_names = grep {!/^\.$/} @file_names; @file_names = grep {!/^\.\.$/} @file_names; # add the full path name back on (used by "stat()" below.) @file_names = map {$_ = $directory . $_} @file_names; # print "file_names: " . Dumper(\@file_names); my %times_to_names; foreach my $name (@file_names) { my $atime = (stat($name))[8]; $times_to_names{$atime} = $name; # print "file: $name\t"; # print "atime: $atime\t"; # print "\n"; } # print "times_to_names: " . Dumper(\%times_to_names); my @times_sorted = sort(keys(%times_to_names)); # print "times_sorted: " . Dumper(\@times_sorted); foreach my $time (@times_sorted) { print $time . "\t" . $times_to_names{$time} . "\n"; } my @lowest_ten_times = @times_sorted[0..9]; # print "lowest_ten_times: " . Dumper(\@lowest_ten_times);