Determine the size of a block device
blockdev --getsize64 /dev/sda
returns size in bytes.
blockdev --getsz /dev/sda
returns size in 512-byte sectors.
Deprecated: blockdev --getsize /dev/sda
returns size in sectors.
blockdev is part of util-linux.
fdisk
doesn't understand the partition layout used by my Mac running Linux, nor any other non-PC partition format. (Yes, there's mac-fdisk
for old Mac partition tables, and gdisk
for newer GPT partition table, but those aren't the only other partition layouts out there.)
Since the kernel already scanned the partition layouts when the block device came into service, why not ask it directly?
$ cat /proc/partitions major minor #blocks name 8 16 390711384 sdb 8 17 514079 sdb1 8 18 390194752 sdb2 8 32 976762584 sdc 8 33 514079 sdc1 8 34 976245952 sdc2 8 0 156290904 sda 8 1 514079 sda1 8 2 155774272 sda2 8 48 1465138584 sdd 8 49 514079 sdd1 8 50 1464621952 sdd2
cat /sys/class/block/sda/size
This gives you its size in 512-byte blocks.