Last active
August 29, 2015 14:23
-
-
Save BrainStone/e801d125843d63afc083 to your computer and use it in GitHub Desktop.
Covert bytes to a human readable format in bash
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#! /bin/bash | |
function humanReadable { | |
if [ $1 -lt 1024 ] | |
then | |
printf "%4i B\n" $1 | |
else | |
postfixes=(KiB MiB GiB TiB EiB PiB YiB ZiB) | |
bytes=$1 | |
count=0 | |
while [ $bytes -ge 1048576 ] | |
do | |
bytes=$((bytes / 1024)) | |
count=$((count + 1)) | |
done | |
printf "%4i,%03i %s\n" $((bytes / 1024)) $(((bytes % 1024) * 1000 / 1024)) ${postfixes[$count]} | |
fi | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment