[BASH] Convert bytes to human readable
function bytesToHR() | |
{ | |
local SIZE=$1 | |
local UNITS="B KiB MiB GiB TiB PiB" | |
for F in $UNITS; do | |
local UNIT=$F | |
test ${SIZE%.*} -lt 1024 && break; | |
SIZE=$(echo "$SIZE / 1024" | bc -l) | |
done | |
if [ "$UNIT" == "B" ]; then | |
printf "%4.0f %s\n" $SIZE $UNIT | |
else | |
printf "%7.02f %s\n" $SIZE $UNIT | |
fi | |
} | |
bytesToHR 1 | |
bytesToHR 1023 | |
bytesToHR 1024 | |
bytesToHR 12345 | |
bytesToHR 123456 | |
bytesToHR 1234567 | |
bytesToHR 12345678 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This comment has been minimized.
This script outputs: