Skip to content

Instantly share code, notes, and snippets.

@BrainStone
Last active August 29, 2015 14:23
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save BrainStone/e801d125843d63afc083 to your computer and use it in GitHub Desktop.
Save BrainStone/e801d125843d63afc083 to your computer and use it in GitHub Desktop.
Covert bytes to a human readable format in bash
#! /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