How can I find out the size of a block device, such as /dev/sda
? Running ls -l
gives no useful information.
views:
1522answers:
8
+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.
2009-06-22 12:53:23
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
2009-06-22 12:53:27
That's only if the filesystem is mounted.
lfaraone
2009-06-22 12:54:45
+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
2009-06-22 17:50:35
+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
2010-04-28 05:56:56
+2
A:
blockdev --getsize64 /dev/sda
returns size in bytes.
blockdev --getsize /dev/sda
returns size in sectors.
David
2010-05-10 13:32:45