File size distribution: Difference between revisions
Content added Content deleted
(UNIX shell: fix wrong sort for files more than 1GB and "prettyfy" the output) |
(UNIX shell: further simplification, only need one awk invocation) |
||
Line 1,605: | Line 1,605: | ||
{{works with|Bourne Shell}} |
{{works with|Bourne Shell}} |
||
Use POSIX conformant code unless the environment variable GNU is set to anything not empty. |
Use POSIX conformant code unless the environment variable GNU is set to anything not empty. |
||
<lang sh> |
<lang sh>#!/bin/sh |
||
#!/bin/sh |
|||
set -eu |
|||
#!/bin/sh |
|||
set -eu |
set -eu |
||
⚫ | |||
if [ ${GNU:-} ] |
if [ ${GNU:-} ] |
||
then |
then |
||
find -- "${1:-.}" -type f -exec du -b -- {} + |
|||
else |
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 ' |
fi | awk -vOFS='\t' ' |
||
⚫ | |||
{ |
|||
{ |
|||
++hist[$1 ? length($1) - 1 : -1] |
|||
total += $1 |
|||
} |
|||
} |
|||
END { |
|||
print total, NR |
|||
max = -2 |
|||
⚫ | |||
for (i in hist) |
|||
max = (i > max ? i : max) |
|||
}' | \ |
|||
{ |
|||
⚫ | |||
read total |
|||
for (i = -1; i <= max; ++i) |
|||
⚫ | |||
{ |
|||
sort -n | awk -vtotal="$total" -vOFS='\t' ' |
|||
⚫ | |||
BEGIN { |
|||
{ |
|||
⚫ | |||
if (i == -1) |
|||
⚫ | |||
print "0B", "0B", hist[i] |
|||
} |
|||
else |
|||
$1 == -1 {print "0B", "0B", $2; next} |
|||
print 10 ** (i % 3) u[int(i / 3)], |
|||
{ |
|||
10 ** ((i + 1) % 3) u[int((i + 1) / 3)], |
|||
hist[i] |
|||
} |
|||
$2 |
|||
} |
|||
} |
|||
⚫ | |||
END { |
|||
⚫ | |||
$0 = total |
|||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
{{out}} |
{{out}} |
||
<pre>$ time ~/fsd.sh |
<pre>$ time ~/fsd.sh |