How to convert Kilobytes to Megabytes or gigabytes through terminal?

I try to find the size of my disk, so i runned the below command

$ sudo fdisk -s /dev/sda
976762584

It shows like above. I think the size(976762584) of the disk is shown in kilobytes. How do i convert the value to megabytes or gigabytes through terminal for better understanding?

3

7 Answers

numfmt (part of GNU Coreutils) can be used here:

$ sudo fdisk -s /dev/sda | numfmt --to=iec-i --suffix=B --format="%.2f"
931.52MiB
1

The shell does fixed-width integer arithmetic with no check for overflow. So, when doing a calculation that might involve either large numbers or fractions, bc is a good choice. To get megabytes:

$ echo "scale=2; $(sudo fdisk -s /dev/sda6) / 1024" | bc
13641.75

To get gigabytes:

$ echo "scale=2; $(sudo fdisk -s /dev/sda6) / 1024^2" | bc
12.70

The assignment scale=2 tells bc to display two decimal places.

1

In awk

To find the size of the disk in Megabytes,

$ sudo fdisk -s /dev/sda | awk '{$1=$1/1024; print $1,"MB";}'
953870 MB

To find the size of the disk in Gigabytes,

$ sudo fdisk -s /dev/sda | awk '{$1=$1/(1024^2); print $1,"GB";}'
931.513 GB
2

There's a tool called units, which can be used for units conversion:

$ units -o "%.0f" -t "4 gibibytes" "mebibytes"
4096

It is available as a package via apt install units.

It is a fairly standard tool, that can be found installed by default on various other UNIX-like systems (e.g., FreeBSD). Keep in mind that units(1) syntax may differ across implementations available on various operating systems. Here's the same conversion but this time with FreeBSD units(1):

$ units -o "%0.f" -t "4 gigabytes" "megabytes"
4096

If the size is given in Kilobytes, you need to calculate through Bash built-in expressions. Assuming block size = 512B, you have to type:

echo $((`fdisk -s /dev/sda`*512/1024))

This will show disk size in KiB. To go further, just add /1024 to the end of expression:

echo $((`fdisk -s /dev/sda`*512/1024/1024))

This will show disk size in MiB and so on.

5

You can do this natively with Bash and pseudo-floating point numbers. Define this function in your ~/.bashrc and reopen your terminal:

function BytesToHuman() { # read StdIn b=${StdIn:-0}; d=''; s=0; S=(Bytes {K,M,G,T,E,P,Y,Z}iB) while ((b > 1024)); do d="$(printf ".%02d" $((b % 1024 * 100 / 1024)))" b=$((b / 1024)) let s++ done echo "$b$d ${S[$s]}"
} # BytesToHuman ()

Now test it:

$ sudo fdisk -s /dev/sda
976762584
$ sudo fdisk -s /dev/sda | BytesToHuman
931.51 MiB

If you're using Bash or Zsh, this function will perform the calculation for you. Additionally, if -si is the first argument, calculations are performed in SI units.

size () { local -a units local -i scale if [[ "$1" == "-si" ]] then scale=1024 units=(B KiB MiB GiB TiB EiB PiB YiB ZiB) shift else scale=1000 units=(B KB MB GB TB EB PB YB ZB) fi local -i unit=0 if [ -z "${units[0]}" ] then unit=1 fi local -i whole=${1:-0} local -i remainder=0 while (( whole >= $scale )) do remainder=$(( whole % scale )) whole=$((whole / scale)) unit=$(( $unit + 1 )) done local decimal if [ $remainder -gt 0 ] then local -i fraction="$(( (remainder * 10 / scale)))" if [ "$fraction" -gt 0 ] then decimal=".$fraction" fi fi echo "${whole}${decimal}${units[$unit]}"
}

For example:

$ size 1
1B
$ size 100
100B
$ size 999
999B
$ size 1000
1KB
$ size 4096
4KB
$ size -si 1000
1000B
$ size -si 1024
1KiB
$ size -si $(( 1 * 1024 * 1024 ))
1MiB
$ size -si $(( 1 * 1024 * 1024 - 1 ))
1023.9KiB
$ size -si $(( 1 * 1024 * 1024 + 1024 ))
1MiB
$ size -si $(( 1 * 1024 * 1024 + 1024 + 1 ))
1MiB
$ size -si $(( 1 * 1024 * 1024 + ( 1024 * 512 ) ))
1.5MiB
$ size -si $(( 1 * 1024 * 1024 + ( 1024 * 1024 ) ))
2MiB
$ size -si $(( 1 * 1024 * 1024 + ( 1024 * 1024 - 1 ) ))
1.9MiB

This is also compatible with e.g. sort -h.

$ { size 32; size 32000; size 35 } | sort -h
32B
35B
32KB

Your Answer

Sign up or log in

Sign up using Google Sign up using Facebook Sign up using Email and Password

Post as a guest

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

You Might Also Like