How to resolve the algorithm File size distribution step by step in the UNIX Shell programming language

Published on 12 May 2024 09:40 PM

How to resolve the algorithm File size distribution step by step in the UNIX Shell programming language

Table of Contents

Problem Statement

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?

Let's start with the solution:

Step by Step solution about How to resolve the algorithm File size distribution step by step in the UNIX Shell programming language

Source code in the unix programming language

#!/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
    }'


  

You may also check:How to resolve the algorithm User input/Graphical step by step in the Nim programming language
You may also check:How to resolve the algorithm System time step by step in the SQL PL programming language
You may also check:How to resolve the algorithm CRC-32 step by step in the Pascal programming language
You may also check:How to resolve the algorithm Horner's rule for polynomial evaluation step by step in the MATLAB programming language
You may also check:How to resolve the algorithm Forward difference step by step in the Smalltalk programming language