Skip to content

Instantly share code, notes, and snippets.

@sanjeevtripurari
Created January 6, 2016 15:10
Show Gist options
  • Save sanjeevtripurari/6a7dbcda15ae5dec7b56 to your computer and use it in GitHub Desktop.
Save sanjeevtripurari/6a7dbcda15ae5dec7b56 to your computer and use it in GitHub Desktop.
Shell script to convert bytes to KB, MB, GB, PB, TB
#!/bin/bash
# Give numeric value, which is in bytes
# will show all possible conversions
call_bc() {
n1=$1
n2=$2
echo "scale=4; $n1/($n2)" |bc
}
nm=$1
pw=$2
pn=""
k_ilo=1024;
m_ega=$k_ilo*$k_ilo;
g_iga=$m_ega*$k_ilo;
t_era=$g_iga*$k_ilo;
p_eta=$t_era*$k_ilo;
[ -z "$nm" ] && exit
echo "Bytes: "$nm
for i in KB MB GB TB PB; do
case $i in
KB) let pn=$k_ilo;;
MB) let pn=$m_ega;;
GB) let pn=$g_iga;;
TB) let pn=$t_era;;
PB) let pn=$p_eta;;
esac
echo -n "$i: "; call_bc $nm $pn
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment