Browse Source

Split out size formatting

master
JustAnotherArchivist 1 year ago
parent
commit
5c654cb16b
2 changed files with 38 additions and 1 deletions
  1. +37
    -0
      format-size
  2. +1
    -1
      warc-size

+ 37
- 0
format-size View File

@@ -0,0 +1,37 @@
#!/bin/bash
# Format size in bytes into a readable string of the form "1.23 KiB"; reads from stdin and arguments and can take multiple numbers separated by any amount of whitespace including newlines
{
if [ ! -t 0 ]; then cat; fi
echo "$@"
} |
tr '\t ' '\n' |
awk '
BEGIN {
units[0] = "B";
units[1] = "KiB";
units[2] = "MiB";
units[3] = "GiB";
units[4] = "TiB";
units[5] = "PiB";
}

{
size += $1;
}

END {
if (size > 0) {
magnitude = int(log(size) / log(1024));
if (magnitude > 5) {
magnitude = 5;
}
} else {
magnitude = 0;
}
if (magnitude > 0) {
sizeformat = "%.2f";
} else {
sizeformat = "%d";
}
printf sizeformat " %s\n", size / (1024 ^ magnitude), units[magnitude];
}'

+ 1
- 1
warc-size View File

@@ -1,3 +1,3 @@
#!/bin/bash
# Total size of all WARCs in the current directory (or subdirectories)
find -name '*.warc.gz' -printf '%s\n' | awk 'BEGIN { units[0] = "B"; units[1] = "KiB"; units[2] = "MiB"; units[3] = "GiB"; units[4] = "TiB"; units[5] = "PiB"; } { size += $1 } END { if (size > 0) { magnitude = int(log(size) / log(1024)); if (magnitude > 5) { magnitude = 5; } } else { magnitude = 0; } if (magnitude > 0) { sizeformat = "%.2f"; } else { sizeformat = "%d"; } printf sizeformat " %s\n", size / (1024 ^ magnitude), units[magnitude]; }'
find -name '*.warc.gz' -printf '%s\n' | "$(cd "$(dirname "$0")"; pwd -P)/format-size"

Loading…
Cancel
Save