From ff9a196ad3df7bd5881de3c7c32494398f05473d Mon Sep 17 00:00:00 2001 From: =?utf8?q?Paul=20H=C3=A4nsch?= Date: Mon, 21 Mar 2022 14:58:04 +0100 Subject: [PATCH] calculation of human readable file sizes --- tools.sh | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) create mode 100644 tools.sh 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 +} -- 2.39.2