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?




using Humanize
 
function sizelist(path::AbstractString)
    rst = Vector{Int}(0)
    for (root, dirs, files) in walkdir(path)
        files = joinpath.(root, files)
        tmp = collect(filesize(f) for f in files if !islink(f))
        append!(rst, tmp)
    end
    return rst
end
 
byclass(y, classes) = Dict{eltype(classes),Int}(c => count(c[1] .≤ y .< c[2]) for c in classes)
 
function main(path::AbstractString)
    s = sizelist(path)
    cls = append!([(0, 1)], collect((10 ^ (i-1), 10 ^ i) for i in 1:9))
    f = byclass(s, cls)
 
    println("filesizes: ")
    for c in cls
        @printf(" - between %8s and %8s bytes: %3i\n", datasize(c[1]), datasize(c[2]), f[c])
    end
    println("\n-> total: $(datasize(sum(s))) bytes and $(length(s)) files")
end
 
main(".")