20

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
  • 1
    This would do the trick: df -h /. Display the size of / in a -human readable format.
    – blade19899
    Feb 16, 2016 at 14:51
  • fdisk -s is also deprecated so probably better to use blockdev --getsize64
    – user986805
    Oct 9, 2019 at 4:55
  • @blade19899 that will get the size of the partition not the whole disk
    – user986805
    Oct 9, 2019 at 4:58

7 Answers 7

16

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

$ sudo fdisk -s /dev/sda
976762584

$ sudo fdisk -s /dev/sda | numfmt --to=iec-i --suffix=B --format="%.2f"
931.52MiB
3
  • 1
    Including the output from the command in your answer would make it more complete. Oct 8, 2019 at 23:03
  • @Samir adding the output as an example here only makes sense if the output of sudo fdisk s /dev/sda and sudo fdisk -s /dev/sda | numfmt --to=iec-i --suffix=B --format="%.2f" agree. Now we have an example where the outputs have no relation to each other. numfmt isn't going to convert 488386584 to 931.52MiB
    – muru
    Oct 31, 2022 at 6:21
  • Yes you're correct, I updated it with the correct number. Nov 4, 2022 at 9:22
12

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
  • I like using herestrings for cases like this: bc <<<"scale=2; $(sudo fdisk -s /dev/sda6) / 1024^2"
    – dimo414
    Aug 23, 2017 at 21:31
5

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
  • instead of sudo fdisk -s /dev/sda you can use df | grep ^\/dev.*/\$ Jun 19, 2015 at 21:53
  • @DeanRather sudo fdisk was the project scope defined in question. Oct 8, 2019 at 23:04
4

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
3

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
  • 2
    Nicely done. As a minor note, the $[...] format has been deprecated and, as man bash says, "will be removed in upcoming versions." The currently preferred format for shell arithmetic is $((...)).
    – John1024
    Apr 25, 2014 at 4:37
  • @John1024 thanks for the note. Didn't guess that double brackets syntax is appropriate.
    – Danatela
    Apr 25, 2014 at 4:44
  • Copy and pasting two commands generate error because sudo is missing. Also the unit of measure doesn't print out. Oct 8, 2019 at 23:07
  • measurement is wrong.
    – user986805
    Oct 9, 2019 at 6:02
  • but you could do echo $((($(cat /sys/block/sda/size)*512)/1024**3))GB
    – user986805
    Oct 9, 2019 at 6:03
1

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

function BytesToHuman() {

    # https://unix.stackexchange.com/questions/44040/a-standard-tool-to-convert-a-byte-count-into-human-kib-mib-etc-like-du-ls1/259254#259254

    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
0

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

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .