Take the 2-minute tour ×
Ask Ubuntu is a question and answer site for Ubuntu users and developers. It's 100% free, no registration required.

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?

share|improve this question

3 Answers 3

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
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.

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

Your Answer

 
discard

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.