Ubuntu – How to find the size of a filesystem

btrfsfilesystemlvmpartitioning

How can I find out the size of a filesystem in Linux? By that I mean the exact number of bytes that are used from the partition, not just the output of df, as that can differ from the true size when compression or deduplication is used on the filesystem.

The size of the partition itself can be printed with:

$ lsblk -b

or

$ blockdev --getsize64 /dev/sda

I am looking for something similar for the filesystem.

PS: This is for LVM grow/shrink stuff. I am mostly interested in ext2/3/4 and btrfs, but any other filesystem info is appreciated as well.

Best Answer

If you want filesystem information and not partition/volume information, I think you'll have to use filesystem-specific tools.

In the case of the extN systems, that would be dumpe2fs. And dumpe2fs doesn't directly print the size in bytes, as far as I can tell. It does, however, print the block count and the size of blocks, so you can parse the output instead:

$ dumpe2fs -h /dev/sda1 |& awk -F: '/Block count/{count=$2} /Block size/{size=$2} END{print count*size}'           
29999980544

In my case, this size is slightly different from the partition size:

$ parted /dev/sda u b p
Model: ATA ST500LT012-1DG14 (scsi)
Disk /dev/sda: 500107862016B
Sector size (logical/physical): 512B/4096B
Partition Table: gpt
Disk Flags: 

Number  Start          End            Size           File system     Name  Flags
 1      17408B         30000000511B   29999983104B   ext4                  boot, esp
 2      30000807936B   453049843711B  423049035776B  ext4
 5      453049843712B  495999516671B  42949672960B   ext4
 3      495999516672B  500102862847B  4103346176B    linux-swap(v1)
 4      500102862848B  500107845119B  4982272B                             bios_grub

The partition size is 29999983104 bytes, 2560 bytes more than a multiple of the block size, which is why the size reported by dumpe2fs is less.

Related Question