Beefy Boxes and Bandwidth Generously Provided by pair Networks
Pathologically Eclectic Rubbish Lister
 
PerlMonks  

Re: finding top 10 largest files

by pelagic (Curate)
on Feb 03, 2004 at 12:07 UTC ( #326175=note: print w/ replies, xml ) Need Help??


in reply to finding top 10 largest files

And yet annother possibility I found in my code stash.
It lists the top n filesizes with all files having these sizes:

#!/usr/bin/perl use strict; my ($dir_init, $top_n) = @ARGV; my (@ls, @dirs, %top, $size, $obsolete_size); my $sep = "~" x 80; my $form = "%20s %s\n"; my $min_size = 0; my $files = 0; push(@dirs, $dir_init);
foreach my $directory (@dirs) { opendir (DIR, $directory) or die ("Can't open dir: '$directory'.\n +Reason: $!"); @ls = readdir(DIR); closedir (DIR); FILE: foreach my $file (@ls) { next if ($file =~ m/^\.\.?$/); $files++; SWITCH: { if (-d "$directory/$file") { push(@dirs, "$directory/$file"); last SWITCH; } if (-l "$directory/$file") { last SWITCH; } if (-f "$directory/$file") { $size = (-s "$directory/$file"); if ($size >= $min_size) { push (@{$top{$size}}, "$directory/$file"); if ($size > $min_size && scalar(keys %top) > $top_ +n) { ($obsolete_size,$min_size) = (sort {$a <=> $b +} keys %top)[0,1]; delete($top{$obsolete_size}); } } last SWITCH; } else { print "\n$file\tis of UNKNOWN type"; } } } } print $sep . "\n"; print "Total number of files examined: $files\n"; print $sep . "\n"; print "List of top " . $top_n . " sized files within " . $dir_init . " + :\n"; foreach my $k (sort {$b <=> $a} keys %top) { print delim1000($k) . "\n"; foreach (@{$top{$k}}) { print "\t$_\n"; } } print $sep . "\n"; sub delim1000 { my $text = reverse $_[0]; $text =~ s/(\d\d\d)(?=\d)(?!\d*\.)/$1,/g; return scalar reverse $text; }
call it like:
perl fszTop10.pl /u2/integ 10
and the output looks like:
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Total number of files examined: 2331 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ List of top 10 sized files within /u2/integ/t096351 : 23,836,788 /u2/integ/t096351/tmp/PDF/NV61DE.PDF 5,731,355 /u2/integ/t096351/test/gsp_toCMT.tar.gz 180,042 /u2/integ/t096351/jcs553_functions_ALL.sql.v1 /u2/integ/t096351/jcs553_functions_ALL.sql.v2 /u2/integ/t096351/jcs553_functions_ALL.sql.copy 105,520 /u2/integ/t096351/ccm_ui.log 56,216 /u2/integ/t096351/ccm_eng.log 24,072 /u2/integ/t096351/dead.letter 14,506 /u2/integ/t096351/.ccm.ini 11,492 /u2/integ/t096351/.sh_history 5,111 /u2/integ/t096351/.dtprofile 2,561 /u2/integ/t096351/.Xauthority ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

pelagic

Update
I had to fix a bug I found, as I was using the script again.


Comment on Re: finding top 10 largest files
Select or Download Code

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others cooling their heels in the Monastery: (3)
As of 2014-08-30 19:04 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The best computer themed movie is:











    Results (293 votes), past polls