Ask Ubuntu is a question and answer site for Ubuntu users and developers. It's 100% free.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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

$ sudo fdisk -s /dev/sda

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?

share|improve this question
This would do the trick: df -h /. Display the size of / in a -human readable format. – blade19899 Feb 16 at 14:51
up vote 2 down vote accepted

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

To get gigabytes:

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

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

share|improve this answer

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.

share|improve this answer
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 '14 at 4:37
@John1024 thanks for the note. Didn't guess that double brackets syntax is appropriate. – Danatela Apr 25 '14 at 4:44

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
share|improve this answer
instead of sudo fdisk -s /dev/sda you can use df | grep ^\/dev.*/\$ – Dean Rather Jun 19 '15 at 21:53

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.