Task

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?




package require fileutil::traverse
namespace path {::tcl::mathfunc ::tcl::mathop}
 
# Ternary helper
proc ? {test a b} {tailcall if $test [list subst $a] [list subst $b]}
 
set dir [? {$argc} {[lindex $argv 0]} .]
fileutil::traverse Tobj $dir \
	-prefilter {apply {path {ne [file type $path] link}}} \
	-filter    {apply {path {eq [file type $path] file}}}
Tobj foreach path {
	set size [file size $path]
	dict incr hist [? {$size} {[int [log10 $size]]} -1]
}
Tobj destroy
 
foreach key [lsort -int [dict keys $hist]] {
	puts "[? {$key == -1} 0 {1e$key}]\t[dict get $hist $key]"
}