in reply to finding top 10 largest files

and here is my try. the first param is the directory to search.
#!/usr/bin/perl use File::Find; File::Find::find( { wanted => sub { return unless -f; my $s = -s _; return if $min < $s && @z > 10; push @z, [ $File::Find::name, $s ]; @z = sort { $b->[1] <=> $a->[1] } @z; pop @z if @z > 10; $min = $z[-1]->[1]; } }, shift || '.' ); for (@z) { print $_->[0], " ", $_->[1], "\n"; }
Boris

Replies are listed 'Best First'.
Re: Re: finding top 10 largest files
by jdporter (Paladin) on Feb 03, 2004 at 03:38 UTC
    My idea is very similar to both borisz's and Abigail's. borisz's solution has some "issues":
    1. the return line should be
      return if $s < $min && @z == 10;
    2. the numeric sort is inefficient because it entails several perl ops. My solution uses GRT for maximum efficiency.
    File::Find::find( sub { return unless -f; @z = sort @z, sprintf( "%32d:", -s ) . $File::Find::name; shift @z if @z > 10; }, $dir ); print "$_\n" for @z;

    jdporter
    The 6th Rule of Perl Club is -- There is no Rule #6.

Re^2: finding top 10 largest files
by Anonymous Monk on Feb 24, 2016 at 12:07 UTC
    Hi Boris, You perl code searches top 10 files on all sub directories of different filesystem. Example., I have /var and /var/log filesystems, my requirement is to search top 10 files under /var but your code searches through /var/log too. is there a way to restrict this in your script.?