Beefy Boxes and Bandwidth Generously Provided by pair Networks
good chemistry is complicated,
and a little bit messy -LW

Re: finding top 10 largest files

by pelagic (Priest)
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 /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 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~


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

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://326175]
[shmem]: ah. Introduced in 5.22.0, see perl5220delta
[Discipulus]: if so the previous error is a bit misleading, anyway
[shmem]: well, previous to that << was always treated as the left-shift operator
[Eily]: or the here-doc operator
[Eily]: which is actually how it is parsed in this case
[shmem]: older versions of perl can't guess that "use of <<>> may be implemented someday" :D
Discipulus how much wise my brothers are? ;=)
[karlgoethebier]: thanks ;-
[karlgoethebier]: all for reading the manuals

How do I use this? | Other CB clients
Other Users?
Others surveying the Monastery: (9)
As of 2017-07-21 09:09 GMT
Find Nodes?
    Voting Booth?
    I came, I saw, I ...

    Results (320 votes). Check out past polls.