How to find the size of a filesystem?

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.


Solution 1:

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.

Solution 2:

It depends on the filesystem you want to investigate:

ext2, ext3, ext4 file system

dumpe2fs -h /dev/sda1 | grep '^Block'

You have to multiply block count and block size to get number of bytes.

ISO-9660 file system

isoinfo -d -i ubuntu-18.10-desktop-amd64.iso | grep -A1 '^Logical block size'

The unit for volume size is logical blocks, so you have to multiply volume size and logical block size to get number of bytes.

BTRFS file system

As one btrfs can include several devices, the command takes the mountpoint as argument

btrfs filesystem usage -b /mnt/mountpoint | grep 'Device size'