Home Blog Certs Knowledge Base About

du / df / lsof

Disk usage and open file tools: df (filesystem usage), du (directory sizes), lsof (open files, network connections, deleted-but-held files).

df โ€” filesystem usage

df commands
CommandDescription
df -hHuman-readable sizes (GiB, MiB)
df -HSI units (powers of 1000)
df -hTInclude filesystem type column
df -iInode usage instead of blocks
df /homeUsage for a specific mount point
df -x tmpfs -x devtmpfsExclude virtual filesystems
df -lLocal filesystems only (skip NFS)
df --output=source,fstype,size,used,avail,pcent,targetCustom columns

A full disk (100% Use%) with many inodes but 0 inode usage left will also block writes. Check both: df -h and df -i.

du โ€” directory sizes

du commands
CommandDescription
du -sh /var/logTotal size of a directory
du -sh *Size of each item in current directory
du -h --max-depth=1 /varOne level deep, human-readable
du -sh * | sort -rh | head -20Top 20 largest items
du -sh /home/* | sort -rhHome directories by size
du -ah / 2>/dev/null | sort -rh | head -30Top 30 largest files/dirs system-wide
du --exclude="*.mp4" -sh /homeExclude a pattern from count
du -sh /proc /sys /devVirtual filesystem sizes (near 0)
du -sk * | sort -rn | head -10Top 10 by size in KB (numeric sort)

-s = summary (one line per argument) ยท -h = human-readable ยท -a = all files, not just directories ยท --max-depth=N limits recursion.

lsof โ€” open files

Files & processes
CommandDescription
lsof /var/log/syslogWhich processes have this file open
lsof -u aliceAll files opened by user alice
lsof -p 1234All files opened by PID 1234
lsof -c nginxFiles opened by processes named nginx
lsof +D /var/wwwAll files open under a directory (recursive)
lsof -d 4Files with file descriptor 4
lsof -t /var/log/auth.logPIDs only โ€” useful in kill scripts
lsof -u alice -c vimAND โ€” alice AND vim
lsof -u alice -c vim -aAND explicit โ€” alice AND vim (with -a)
Network connections
CommandDescription
lsof -iAll network connections
lsof -i :80Processes using port 80
lsof -i TCP:22TCP connections on port 22
lsof -i @10.0.0.1Connections to a specific host
lsof -i TCP -s TCP:LISTENListening TCP ports only
lsof -i UDPUDP sockets
lsof -i 4IPv4 only
lsof -i 6IPv6 only
Deleted files (disk not freed)
CommandDescription
lsof | grep deletedFiles deleted but still held open โ€” disk space not freed until process closes them
lsof -p 1234 | grep REGRegular files open by a process
ls -la /proc/1234/fd/All file descriptors of a process
cp /proc/1234/fd/3 /tmp/recoveredRecover a deleted file via /proc fd link
kill -HUP $(lsof -t /var/log/app.log)Signal process to reopen log file

Common use case: df -h shows 100% full but du -sh /* doesn’t add up โ†’ a deleted file is still held open. Find it with lsof | grep deleted, then restart or HUP the process.