← Linux and the Command Line
Lesson 26 of 51 · 5 XP
Disk: df and du
Theory
Machine translation, not yet reviewed by a native speaker.
df (disk free) shows how much space there is on the partitions:
df -h # -h: sizes in a human-readable form (G, M)
df -h / # only the root partition
Filesystem Size Used Avail Use% Mounted on
/dev/sda1 40G 31G 7.2G 82% /
du (disk usage) shows how much space files and folders take up:
du -sh ~/Downloads # the total for a folder (-s — summary)
du -h --max-depth=1 /var # by subfolders, one level deep (GNU)
du -sh * | sort -h # what takes up the most space in the current folder
A classic situation on a server: "the disk is full". What to do:
df -h— which partition is full;du -sh /* 2>/dev/null | sort -h— which folder has grown (most often/var/log,/tmp, the Docker cache);- go deeper until you find the culprit.
Sometimes df shows a full disk, but du doesn't find any big files. That means a deleted file is still held open by some program: the space is freed only after that program restarts. lsof | grep deleted helps find such files.
Try it yourself: Programmers' Room → Linux · sh.