tags:

views:

1522

answers:

8

How can I find out the size of a block device, such as /dev/sda? Running ls -l gives no useful information.

+5  A: 

How about:

fdisk -l

This will give you a list of all your disks with their respective capacity, usage, and more.

Wadih M.
A: 
df -k | grep /dev/sda

will give you the size in KB (first number) as well as the space used (second number) and space available (third number)

RichieHindle
That's only if the filesystem is mounted.
lfaraone
+7  A: 

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, but that's not the only other partition layout 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
ephemient
+1: thanks for the addl info.
Wadih M.
This doesn't work for things like CD drives
Ganesh Sittampalam
+2  A: 
cat /sys/class/block/sda/size

This gives you its size in 512-byte blocks.

+1  A: 

echo `cat /sys/class/block/sda2/size`*512 | bc

gives size in byte

ray
+2  A: 

blockdev --getsize /dev/sda

Vi
+2  A: 

This simple code. Couldn't find any documentation, but does the trick nicely:

#include <linux/fs.h>
...
ioctl(file, BLKGETSIZE64, &file_size_in_bytes);
Araneidae
+2  A: 

blockdev --getsize64 /dev/sda returns size in bytes.

blockdev --getsize /dev/sda returns size in sectors.

David