From: Paul Hänsch Date: Mon, 21 Mar 2022 13:58:04 +0000 (+0100) Subject: calculation of human readable file sizes X-Git-Url: https://git.plutz.net/?a=commitdiff_plain;h=ff9a196ad3df7bd5881de3c7c32494398f05473d;p=shellwiki calculation of human readable file sizes --- diff --git a/tools.sh b/tools.sh new file mode 100644 index 0000000..591e9bc --- /dev/null +++ b/tools.sh @@ -0,0 +1,21 @@ +#!/bin/sh + +size_human(){ + local size="$1" + + if [ $size -gt $((1024 * 1024 * 1024)) ]; then + size=$((size / 1024 / 1024 / 1024 * 10 + size / 1024 / 1024 % 1024 / 100)) + printf "%i.%i GB" "$((size / 10))" "$((size % 10))" + + elif [ $size -gt $((1024 * 1024)) ]; then + size=$((size / 1024 / 1024 * 10 + size / 1024 % 1024 / 100)) + printf "%i.%i MB" "$((size / 10))" "$((size % 10))" + + elif [ $size -gt $((1024)) ]; then + size=$((size / 1024 * 10 + size % 1024 / 100)) + printf "%i.%i KB" "$((size / 10))" "$((size % 10))" + + else + printf "%i B" "$size" + fi +}