df, and the output appears almost instantly:
(FS Size Used Avail Use%) /dev/sda1 145G 8.4G 130G 7%
sda1 is an ext4 partition.
Without summing the size of all files, how can
df give me the space information almost instantly?
Like traditional Unix File Systems, ext2, ext3 and ext4 have a segment of metadata called a superblock, which contains information about the configuration of the file system. The primary superblock is stored at a fixed offset from the start of the partition, and since the information it contains is so important, backup copies of the superblock are stored throughout the file system.
The information the superblock contains includes the total number of inodes and blocks in the filesystem and how many are free. This information can be used to calculate the used and available space of the file system efficiently.