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?




#!/bin/sh
set -eu
 
tabs -8
if [ ${GNU:-} ]
then
    find -- "${1:-.}" -type f -exec du -b -- {} +
else
    # Use a subshell to remove the last "total" line per each ARG_MAX
    find -- "${1:-.}" -type f -exec sh -c 'wc -c -- "$@" | sed \$d' argv0 {} +
fi | awk -vOFS='\t' '
    BEGIN {split("KB MB GB TB PB", u); u[0] = "B"}
    {
        ++hist[$1 ? length($1) - 1 : -1]
        total += $1
    }
    END {
        max = -2
        for (i in hist)
            max = (i > max ? i : max)
 
        print "From", "To", "Count\n"
        for (i = -1; i <= max; ++i)
        {
            if (i in hist)
            {
                if (i == -1)
                    print "0B", "0B", hist[i]
                else
                    print 10 ** (i       % 3) u[int(i       / 3)],
                          10 ** ((i + 1) % 3) u[int((i + 1) / 3)],
                          hist[i]
            }
        }
        l = length(total) - 1
        printf "\nTotal: %.1f %s in %d files\n",
            total / (10 ** l), u[int(l / 3)], NR
    }'