Filesystems

Published on:

Most important commands to remember

  • df -h PATH — check space on the filesystem containing a path.
  • du -h FILE — check the storage allocated to a file.
  • stat FILE — inspect a file’s size and metadata.

Commands and flags

These commands appear in the short example below.

Command Meaning
df -h /etc/hosts Show capacity, usage, and available space for the filesystem containing this file.
du -h /etc/hosts Show this file’s allocated space.
stat /etc/hosts Show file metadata, including logical size and allocated blocks.

For GNU df and du, -h selects readable binary units: K means KiB and M means MiB. One KiB is 1,024 bytes; one MiB is 1,024 KiB. /etc/hosts is an existing system file used for local hostname mappings; we only inspect it.

The concepts that matter

1. A filesystem organizes storage

A disk provides storage capacity. A filesystem organizes files, directories, and metadata such as ownership and timestamps. ext4 is one common Linux filesystem.

A mount point connects a filesystem to a path in Linux’s directory tree. Different paths can lead to different filesystems. Space available on one filesystem does not automatically help an application writing to another full filesystem.

2. Filesystem capacity and file usage answer different questions

df describes the containing filesystem. Its Size, Used, and Avail columns show capacity, usage, and space available to ordinary users. Mounted on shows where that filesystem is attached.

du describes the files you ask it to inspect. For a directory, it can walk through the contents and total their allocated space.

Their totals need not match. Filesystem metadata, reserved space, and deleted files still held open by processes can account for differences. Deleting a file does not always free its storage immediately. If a process still has the file open, Linux keeps the file’s data until that process closes it.

3. A file’s length can differ from its allocated space

Logical size is the length of the file’s contents, measured in bytes. Allocated space is the storage assigned to hold those contents.

A tiny file may occupy a larger allocation unit. In the other direction, a sparse file can have a large logical size while using little storage: unwritten regions can read as zeros without a data block for every byte. Sparse regions occupy logical positions in the file but do not reserve physical storage. Future writes into those regions still need free filesystem space and can fail if none remains.

That distinction matters when copying disk images or planning capacity. Future writes can increase allocation without increasing the file’s logical length.

4. Metadata describes the file

stat shows more than size. An inode identifies the file within its filesystem and holds metadata. A filename is a directory entry referring to an inode; multiple names can refer to the same one through hard links.

Focus first on Size, Blocks, ownership, and permissions. For example, suppose stat shows:

  • Size: 1000 — the file contains 1,000 logical bytes.
  • Blocks: 8 — it occupies 8 × 512 = 4,096 bytes of storage.
  • IO Block: 4096 — the filesystem prefers I/O operations in chunks around 4,096 bytes.

IO Block is mainly an efficiency hint.

One small example

Optional: run these commands on Linux. No administrator access is needed:

df -h /etc/hosts
du -h /etc/hosts
stat /etc/hosts

Read Avail in df, then compare du with Size and Blocks in stat. Multiply Blocks by 512 to get allocated bytes. Compare equivalent units before drawing conclusions; readable sizes are rounded, and allocation depends on the filesystem.

Your device, mount point, sizes, and inode will vary. These commands inspect an existing file without changing it, so no cleanup is needed.

Keep this idea: df answers “how much room is left here?”, du answers “how much storage is allocated?”, and stat describes the file itself.