File size distribution: Difference between revisions

→‎{{header|Perl 6}}: Add a Perl 6 example
(Added Sidef)
(→‎{{header|Perl 6}}: Add a Perl 6 example)
Line 2:
 
Beginning from the current directory, or optionally from a directory specified as a command-line argument, determine how many files there are of various sizes in a directory hierarchy. My suggestion is to sort by logarithmn of file size, since a few bytes here or there, or even a factor of two or three, may not be that significant. Don't forget that empty files may exist, to serve as a marker. Is your file system predominantly devoted to a large number of smaller files, or a smaller number of huge files?
 
=={{header|Perl 6}}==
{{works with|Rakudo|2017.05}}
By default, process the current and all readable sub-directories, or, pass in a directory path at the command line.
 
<lang perl6>sub MAIN($dir = '.') {
sub log10 (Int $s) { $s ?? $s.log(10).Int !! 0 }
my %fsize;
my @dirs = $dir.IO;
while @dirs {
for @dirs.pop.dir -> $path {
%fsize{$path.s.&log10}++ if $path.f;
@dirs.push: $path if $path.d and $path.r
}
}
my $max = %fsize.values.max;
my $bar-size = 80;
say "File size distribution in bytes for directory: $dir\n";
say sprintf( "# Files @ 0b %8s: ", %fsize{0} // 0 ),
histogram( $max, %fsize{0} // 0, $bar-size );
for 1 .. %fsize.keys.max {
say sprintf( "# Files @ %5sb %8s: ", "10e{$_-1}", %fsize{$_} // 0 ),
histogram( $max, %fsize{$_} // 0, $bar-size )
}
say %fsize.values.sum, ' total files.';
}
 
sub histogram ($max, $value, $width = 60) {
my @blocks = <| ▏ ▎ ▍ ▌ ▋ ▊ ▉ █>;
my $scaled = ($value * $width / $max).Int;
my ($end, $bar) = $scaled.polymod(8);
(@blocks[8] x $bar * 8) ~ (@blocks[$end] if $end) ~ "\n"
}</lang>
 
{{out}}
<pre>File size distribution in bytes for directory: /home
 
# Files @ 0b 989: ▏
 
# Files @ 10e0b 6655: ████████
 
# Files @ 10e1b 31776: ████████████████████████████████████████
 
# Files @ 10e2b 63165: ████████████████████████████████████████████████████████████████████████████████
 
# Files @ 10e3b 19874: ████████████████████████▏
 
# Files @ 10e4b 7730: ████████▏
 
# Files @ 10e5b 3418: ▌
 
# Files @ 10e6b 1378: ▏
 
# Files @ 10e7b 199:
 
# Files @ 10e8b 45:
 
135229 total files.</pre>
 
=={{header|Python}}==
10,333

edits