df and du
How much space is free on filesystems (df) and how much directories and files take up (du)
df (disk free) answers "how much space is left on the partition" — it asks the filesystem for a summary. du (disk usage) walks directories and adds up file sizes — it answers "what took up the space". Their numbers don't have to match, and that's normal.
Syntax
df [options] [file…] # no arguments — all mounted filesystems
du [options] [path…] # no arguments — the current directory
df path shows the filesystem the path is on.
df output
df -h /
# Filesystem Size Used Avail Use% Mounted on
# overlay 95G 90G 922M 99% /
| Column | Meaning |
|---|---|
Filesystem | device or type (/dev/sda1, overlay, tmpfs) |
Size | total size |
Used | used |
Avail | available to a regular user |
Use% | Used / (Used + Avail), rounded up |
Mounted on | mount point |
Type | filesystem type (with -T) |
Inodes, IUsed, IFree, IUse% | inodes (with -i) |
Used + Avail is usually less than Size: on ext4 about 5% is reserved for root.
df options
| Option | What it does |
|---|---|
-h | sizes in K, M, G (powers of 1024) |
-H | same in powers of 1000 (as on the drive's box) |
-k / -B1M / -BG | size in KiB / MiB / GiB |
-T | add a filesystem type column |
-i | show inodes instead of blocks |
-t ext4 | only filesystems of this type |
-x tmpfs | exclude a type (can be repeated: -x tmpfs -x devtmpfs) |
-l | only local filesystems, no network ones |
-a | include pseudo-filesystems (proc, sysfs…) |
--output=source,fstype,size,used,avail,pcent,target | choose columns; also itotal, iused, iavail, ipcent, file |
--total | add a total line |
-P | POSIX format: one filesystem is always one line (handy for awk) |
du options
| Option | What it does |
|---|---|
-s | only a total for each argument, no subdirectories |
-h | sizes in K, M, G; --si — in powers of 1000 |
-a | show files too, not only directories |
-d N / --max-depth=N | no deeper than N levels (-d 1 — immediate subdirectories) |
-c | add a total line |
-x | don't cross into other filesystems (important for du /) |
-k / -m / -B1G | units: KiB / MiB (rounded up) / GiB |
-b | exact size in bytes (--apparent-size + -B1) |
--apparent-size | the "apparent" file size, not the blocks used on disk |
-S | don't include subdirectories in a directory's size |
-t 100M | show only entries above the threshold (-t -1M — below) |
--exclude='*.log' | skip files matching a pattern; -X file — patterns from a file |
--time | add the time of the last modification inside |
--inodes | count files instead of size |
-L | follow symbolic links (doesn't by default) |
-l | count hard links every time (by default a file is counted once) |
-0 | separate lines with a NUL byte |
du output is two tab-separated columns: size and path.
Examples
# free space on all filesystems, without the service ones
df -h -x tmpfs -x devtmpfs
# which partition a directory is on and how much space is there
df -h /var/log
# root usage percentage as a number, for a script
df --output=pcent / | tail -n 1 | tr -dc '0-9'
# out of inodes? (lots of small files)
df -i /
# how much a directory takes
du -sh /var/log
# first-level subdirectories, largest first
du -h -d 1 /var | sort -rh | head
# the largest files and directories inside, without crossing filesystems
du -ahx /var | sort -rh | head -n 20
# several paths and a total
du -csh /var/log /var/cache
# real file size vs. space used (sparse files)
du -h --apparent-size big.img
du -h big.img
# only what's larger than 100 MiB
du -h -t 100M /var
Pitfalls
dfshows space as used, butducan't find that much. Most often it's a deleted but still open file: the space is freed when the process closes it. Look through/proc/*/fd(see proc). Other causes — files hidden under a mount point or in a directory you can't access.- By default
ducounts blocks, not bytes. A 1-byte file takes 4.0K, and a 1 GiB sparse image may take 0. For "file length" use--apparent-sizeor-b. - Hard links are counted once per run.
du -sh a banddu -sh bcan give different numbers forbif its files are links to files ina. sort -h, notsort -n. Only-hunderstands that2Gis greater than900M.du -sh *skips hidden files and directories (.cache,.git): the*pattern doesn't expand to them. Usedu -sh .[!.]* *ordu -d 1 ..- You can run out of space with gigabytes free — if you're out of inodes. The error is the same:
No space left on device. Checkdf -i. du /without-xgoes into/proc,/sysand mounted volumes — slow and with lots of errors. Add-xand2>/dev/null.- In a container
df /shows the overlay filesystem shared with the host — that's space on the host's disk, not "the container's size".