How do you find and manage disk usage in Linux with df and du?
Learn how to find and manage Linux disk usage with df and du, spot full filesystems, catch inode exhaustion, and pinpoint the directories eating your space.
Expected Interview Answer
In Linux, df reports free and used space per mounted filesystem, while du measures how much space specific files and directories actually consume, so you use df to spot which mount is full and du to drill down into what is filling it.
df reads filesystem-level accounting and answers 'how full is each disk', typically run as df -h for human-readable sizes and df -i to inspect inode exhaustion. du walks a directory tree and sums file sizes; du -sh gives one total for a path and du -h --max-depth=1 | sort -h ranks the biggest subdirectories. Together they turn 'the disk is full' into a precise target you can clean up.
- df quickly shows which mount point is out of space
- du pinpoints the exact directories consuming space
- df -i catches inode exhaustion when bytes look fine
- Human-readable -h flags make output easy to scan
- Sorting du output surfaces the biggest offenders fast
AI Mentor Explanation
df is the scoreboard telling you the total runs and how many are left to chase across the whole innings, giving the big picture at a glance. du is the ball-by-ball commentary breaking down exactly which batter scored how many, so when the total looks off you know precisely which partnership inflated it and where to look.
Step-by-Step Explanation
Step 1
Check overall usage
Run df -h to see each mounted filesystem's size, used, available and use percentage in human-readable units.
Step 2
Rule out inode exhaustion
If df -h shows free bytes but writes still fail, run df -i to check whether inodes are exhausted.
Step 3
Target the full mount
Change into the filesystem that df flagged as nearly full so du scans the right tree.
Step 4
Rank subdirectories
Run du -h --max-depth=1 . | sort -h to list immediate subdirectories by size, biggest last.
Step 5
Drill into the offender
Repeat du inside the largest directory until you reach the specific files, then archive or delete safely.
What Interviewer Expects
- Knowing df is filesystem-level and du is path-level
- Using the -h flag for human-readable output
- Awareness of df -i for inode exhaustion
- Combining du with sort to find big directories
- Explaining why df and du totals can differ
Common Mistakes
- Confusing df and du or swapping their purposes
- Ignoring inode exhaustion when bytes look free
- Assuming df and du always report identical totals
- Forgetting -h and misreading raw block counts
- Deleting files without checking open file handles keeping space used
Best Answer (HR Friendly)
“df tells you how full each disk is at a high level, and du shows which folders and files are actually taking up the space. You start with df to find the full disk, then use du to zoom in on what to clean up.”
Code Example
# Overall usage per filesystem, human-readable
df -h
# Check inodes if space looks free but writes fail
df -i
# Biggest immediate subdirectories under current path
du -h --max-depth=1 . | sort -h
# Total size of one directory
du -sh /var/logFollow-up Questions
- Why might df and du report different amounts of used space?
- How do you diagnose a disk that is full on inodes but not bytes?
- What causes a deleted file to keep consuming disk space?
- How would you find the ten largest files under a directory?
MCQ Practice
1. Which command reports free space per mounted filesystem?
df reports size, used and available space for each mounted filesystem; du measures per-directory usage.
2. You have free bytes but writes fail. What should you check?
A filesystem can run out of inodes while bytes remain free; df -i shows inode usage.
3. Which command ranks immediate subdirectories by size?
du --max-depth=1 sums each immediate child, and sort -h orders human-readable sizes.
Flash Cards
What does df measure? — Used and available space per mounted filesystem, at the filesystem level.
What does du measure? — The actual space consumed by specific files and directories in a tree.
What does df -i show? — Inode usage, useful when bytes are free but writes still fail.
How to find biggest subdirectories? — du -h --max-depth=1 . | sort -h
Continue Learning
Related Interview Questions
What is the difference between a hard link and a symbolic link in Linux?
medium
What is the difference between absolute and relative paths in Linux?
easy
How does the chmod octal notation work in Linux (e.g. 755, 644)?
medium
df reports the filesystem is full but du shows far less usage. What is going on?
hard