Skip to content

Instantly share code, notes, and snippets.

@silasdavis
Created August 6, 2015 13:06
Show Gist options
  • Save silasdavis/baf92c89179bce4a56d4 to your computer and use it in GitHub Desktop.
Save silasdavis/baf92c89179bce4a56d4 to your computer and use it in GitHub Desktop.
Convert integral bytes ot human friendly format. Reads number of bytes from stdin or argument or both using '-' for replacement
#!/bin/bash
function human-bytes {
numfmt --to=iec-i --suffix=B --padding=7 "$@"
}
if [ "$#" -eq 0 ]
then
# Is stdin a terminal?
if [ -t 0 ]; then
cat << EOF
Prints bytes in more human-readable form.
Usage: human-bytes bytes [more-bytes]
Where bytes should be a number of bytes, or '-' to read from stdin.
EOF
else
while read line
do
human-bytes $line
done
fi
fi
for arg in "$@"
do
if [ $arg == "-" ]
then
while read line
do
human-bytes $line
done
else
human-bytes $arg
fi
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment