Linux Labs

Storage / Stage 1 of 11

Out of space — which resource?

Not running Starting usually ~20 s
Leave the lesson

Step 3 of 8 · on a machine · 5 min

Size, blocks and the inode of one file

Three different numbers describe the size of one file, and only one of them shows the space it takes.

stat prints all three side by side. This is an empty file from the cache that used up the inodes at the start of the lesson:

text
  Size: 0       Blocks: 0     IO Block: 4096  regular empty file
Device: 7,0     Inode: 16     Links: 1
stat field What it means Also shown by
Size size: how many bytes a program will read ls -l
Blocks space for the contents, in 512-byte units du
Inode the number of the file's record ls -i

Size is the file's apparent size. An empty file has zero size and zero blocks, yet it has an inode. Hundreds of such files barely register in df -h and du -h, while they use up every inode.

The opposite mismatch happens too: a sparse file has a large size and few blocks. Regions nothing was ever written to take no space and read back as zeros. Create one yourself:

bash
truncate -s 1G /tmp/sparse.img
ls -lh /tmp/sparse.img
du -h /tmp/sparse.img

ls -lh shows a size of 1.0G and du -h shows 0: truncate set the size, and nobody wrote any data into the file. The training volume's image is built the same way.

Check yourself 1 question

The root / is running out of space. ls -lhS /var/lib/images lists the image vm.img first, with a size of 40G. What do you do first?

  • The archive needs space of its own, and you free no more than the image occupies according to du. Find that number first: du -h vm.img.

  • A file has one inode whatever its size. df -i helps when file records run out, and here the question is about blocks.

  • The size includes regions nothing was ever written to. du -h vm.img shows how much space the image really takes; for a sparse image that can be a couple of gigabytes.

The size from ls -l includes the unwritten regions of a sparse file, while du shows the space it occupies. Compare du -h vm.img with du -h --apparent-size vm.img: the second returns the apparent size.

Key points

  • A file's size, its allocated blocks and its inode are three separate measures, and stat shows all three.
  • An empty file uses an inode with almost no blocks; a sparse file's size is larger than the space allocated to it.
Ubuntu 24.04
Loading…