Linux commands
Browse topics

Checking disk space with df and du

Version
unversioned
Review
Draft

Summary#

df reports used and available space for whole file systems. du estimates the space used by particular files and directories.

Syntax#

sh
df -h path
df -k --output=fstype,size,used,avail,pcent path
du -sh directory
du --apparent-size --block-size=1 file

-h prints readable units and du -s prints one total per argument. du measures allocated space by default; --apparent-size measures file length.

Example#

sh
(
  set -eu
  export LC_ALL=C
  dir=$(mktemp -d)
  trap 'rm -rf -- "$dir"' EXIT
  cd "$dir"
  head -c 3000 /dev/zero > small.bin
  head -c 50000 /dev/zero > large.bin

  echo '-- apparent size in bytes, largest first'
  du --apparent-size --block-size=1 -- *.bin | sort -rn
  echo '-- selected df columns'
  df -k --output=fstype,size,used,avail,pcent . | head -n 1 | tr -s ' '
  echo '-- df reports only the file system that holds this path'
  df -P . | wc -l
)

Expected output#

Text
-- apparent size in bytes, largest first
50000	large.bin
3000	small.bin
-- selected df columns
Type 1K-blocks Used Avail Use%
-- df reports only the file system that holds this path
2

Common mistakes#

Deleting a large file does not free its space while a process still has it open. In that case df can report much more use than du finds; close the file or restart the process that holds it. Also note that du -sh * leaves out hidden entries because the shell glob skips them.

Version applicability#

This entry describes GNU coreutils df and du. The --output and --apparent-size forms used here are GNU options that BSD and macOS tools may not accept.

Sources#

Attribution
DevLookup contributors
License
CC-BY-4.0