← Full reference

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% /
ColumnMeaning
Filesystemdevice or type (/dev/sda1, overlay, tmpfs)
Sizetotal size
Usedused
Availavailable to a regular user
Use%Used / (Used + Avail), rounded up
Mounted onmount point
Typefilesystem 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

OptionWhat it does
-hsizes in K, M, G (powers of 1024)
-Hsame in powers of 1000 (as on the drive's box)
-k / -B1M / -BGsize in KiB / MiB / GiB
-Tadd a filesystem type column
-ishow inodes instead of blocks
-t ext4only filesystems of this type
-x tmpfsexclude a type (can be repeated: -x tmpfs -x devtmpfs)
-lonly local filesystems, no network ones
-ainclude pseudo-filesystems (proc, sysfs…)
--output=source,fstype,size,used,avail,pcent,targetchoose columns; also itotal, iused, iavail, ipcent, file
--totaladd a total line
-PPOSIX format: one filesystem is always one line (handy for awk)

du options

OptionWhat it does
-sonly a total for each argument, no subdirectories
-hsizes in K, M, G; --si — in powers of 1000
-ashow files too, not only directories
-d N / --max-depth=Nno deeper than N levels (-d 1 — immediate subdirectories)
-cadd a total line
-xdon't cross into other filesystems (important for du /)
-k / -m / -B1Gunits: KiB / MiB (rounded up) / GiB
-bexact size in bytes (--apparent-size + -B1)
--apparent-sizethe "apparent" file size, not the blocks used on disk
-Sdon't include subdirectories in a directory's size
-t 100Mshow only entries above the threshold (-t -1M — below)
--exclude='*.log'skip files matching a pattern; -X file — patterns from a file
--timeadd the time of the last modification inside
--inodescount files instead of size
-Lfollow symbolic links (doesn't by default)
-lcount hard links every time (by default a file is counted once)
-0separate 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

  • df shows space as used, but du can'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 du counts blocks, not bytes. A 1-byte file takes 4.0K, and a 1 GiB sparse image may take 0. For "file length" use --apparent-size or -b.
  • Hard links are counted once per run. du -sh a b and du -sh b can give different numbers for b if its files are links to files in a.
  • sort -h, not sort -n. Only -h understands that 2G is greater than 900M.
  • du -sh * skips hidden files and directories (.cache, .git): the * pattern doesn't expand to them. Use du -sh .[!.]* * or du -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. Check df -i.
  • du / without -x goes into /proc, /sys and mounted volumes — slow and with lots of errors. Add -x and 2>/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".

See also

find · proc · sort · logrotate