Jump to content

File size distribution: Difference between revisions

UNIX shell: fix wrong sort for files more than 1GB and "prettyfy" the output
(Simplify and accelerate UNIX shell solution massively)
(UNIX shell: fix wrong sort for files more than 1GB and "prettyfy" the output)
Line 1,605:
{{works with|Bourne Shell}}
Use POSIX conformant code unless the environment variable GNU is set to anything not empty.
<lang sh>#!/bin/sh
#!/bin/sh
set -eu
 
#!/bin/sh
set -eu
 
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 '
{
{
++hist[$1 ? length($1) - 1 : -1]
total += $1
}
}
END {
print total, NR
for (i in hist)
print (i == -1 ? 0 : "1e" i) "\t" hist[i]
print i, hist[i]
print "Total: " total " bytes in " NR " files"
}' | sort</lang>\
{
read total
tabs -8
sort -n | awk -vtotal="$total" -vOFS='\t' '
BEGIN {
split("KB MB GB TB PB", u); u[0] = "B"
print "From", "To", "Count\n"
}
$1 == -1 {print "0B", "0B", $2; next}
{
print 10 ** ($1 % 3) u[int($1 / 3)],
10 ** (($1 + 1) % 3) u[int(($1 + 1) / 3)],
$2
}
END {
$0 = total
l = length($1) - 1
printf "\nTotal: %.1f %s in %d files\n",
$1 / (10 ** l), u[int(l / 3)], $2}'
}</lang>
{{out}}
<pre>$ time ~/fsd.sh
0From To 4 Count
 
1e0 66
1e10B 66 0B 13
1e21B 1418 10B 74
1e310B 1026100B 269
1e4100B 1KB 1564 5894
1e51KB 6008310KB 12727
1e610KB 100KB 16282 12755
1e7100KB 1MB 3881 110922
1e81MB 144410MB 50019
1e910MB 100MB 16 17706
100MB 1GB 5056
Total: 612404756079 bytes in 85850 files
1GB 10GB 1139
~/fsd.sh 0.60s user 0.98s system 134% cpu 1.182 total
10GB 100GB 141
100GB 1TB 1
 
Total: 6124047560798.9 bytesTB in 85850216716 files
~/fsd.sh 01.60s28s user 02.98s55s system 134% cpu 12.182842 total
$ time GNU=1 ~/fsd.sh
0From To 4 Count
 
1e0 66
1e10B 66 0B 13
1e21B 1418 10B 74
1e310B 1026100B 269
1e4100B 1KB 1564 5894
1e51KB 6008310KB 12727
1e610KB 100KB 16282 12755
1e7100KB 1MB 3881 110922
1e81MB 144410MB 50019
1e910MB 100MB 16 17706
100MB 1GB 5056
Total: 612404756079 bytes in 85850 files
1GB 10GB 1139
GNU=1 ~/fsd.sh 0.35s user 0.48s system 135% cpu 0.613 total</pre>
10GB 100GB 141
100GB 1TB 1
 
Total: 6124047560798.9 bytesTB in 85850216716 files
GNU=1 ~/fsd.sh 0.35s81s user 01.48s33s system 135% cpu 01.613586 total</pre>
 
=={{header|zkl}}==
Anonymous user
Cookies help us deliver our services. By using our services, you agree to our use of cookies.