Bird
Raised Fist0
Linux CLIscripting~15 mins

df (disk free space) in Linux CLI - Deep Dive

Choose your learning style10 modes available
Overview - df (disk free space)
What is it?
The df command in Linux shows how much disk space is used and available on your file systems. It lists each mounted file system with details like total size, used space, and free space. This helps you understand storage usage on your computer or server. It is a simple way to check if your disks are running out of space.
Why it matters
Without df, you would not easily know how much storage space is left on your disks. This could lead to running out of space unexpectedly, causing programs to fail or data loss. Knowing disk usage helps you manage files, clean up space, and plan upgrades. It keeps your system healthy and prevents crashes caused by full disks.
Where it fits
Before learning df, you should understand basic Linux commands and the concept of file systems and storage. After mastering df, you can learn related commands like du (disk usage) for folder sizes, and tools for monitoring disk health and performance.
Mental Model
Core Idea
df reports the current storage usage and availability for each mounted file system on your Linux machine.
Think of it like...
df is like a fuel gauge for your car's gas tank, showing how much fuel (disk space) you have left before you need to refill (free up space).
┌───────────────┬───────────┬───────────┬───────────┬─────────────┐
│ Filesystem    │ Size      │ Used      │ Avail     │ Mounted on  │
├───────────────┼───────────┼───────────┼───────────┼─────────────┤
│ /dev/sda1     │ 100G      │ 60G       │ 40G       │ /           │
│ tmpfs         │ 2G        │ 0G        │ 2G        │ /run        │
└───────────────┴───────────┴───────────┴───────────┴─────────────┘
Build-Up - 7 Steps
1
FoundationBasic df command usage
🤔
Concept: Learn how to run df to see disk space usage for all mounted file systems.
Open a terminal and type: df This shows a table with columns for filesystem, size, used, available space, and mount point.
Result
You see a list of mounted file systems with their total size, used space, and free space.
Understanding the default output of df is the first step to monitoring disk space on your system.
2
FoundationUnderstanding filesystem and mount points
🤔
Concept: Learn what filesystems and mount points mean in the context of df output.
Each line in df output represents a filesystem, which is a storage area formatted to hold files. The mount point is where that filesystem is attached in your directory tree, like / or /home.
Result
You can identify which physical or virtual disk corresponds to which part of your system.
Knowing what filesystems and mount points are helps you interpret df output correctly.
3
IntermediateUsing human-readable sizes
🤔Before reading on: do you think df shows sizes in bytes or a more readable format by default? Commit to your answer.
Concept: Learn to use the -h option to show sizes in KB, MB, or GB for easier reading.
Run: df -h This changes the size columns to use units like G for gigabytes, M for megabytes, making it easier to understand at a glance.
Result
Output shows sizes like 20G instead of large numbers like 21474836480.
Using human-readable sizes prevents mistakes caused by misreading large numbers.
4
IntermediateChecking specific filesystem usage
🤔Before reading on: do you think df can show info for just one filesystem or directory? Commit to your answer.
Concept: Learn to check disk usage for a specific mount point or directory.
Run: df -h /home This shows disk usage only for the filesystem mounted at /home, helping focus on one area.
Result
Output shows size, used, and available space only for /home filesystem.
Targeting specific filesystems helps when managing space on multi-disk systems.
5
IntermediateUnderstanding filesystem types with df
🤔
Concept: Learn to display the type of each filesystem using the -T option.
Run: df -T This adds a column showing filesystem types like ext4, tmpfs, or nfs, which tells you about the storage format or network mount.
Result
Output includes a new column listing filesystem types for each entry.
Knowing filesystem types helps diagnose issues and understand performance or compatibility.
6
AdvancedInterpreting reserved space and root usage
🤔Before reading on: do you think all free space shown by df is available to all users? Commit to your answer.
Concept: Learn about reserved blocks that only root can use and how df reports them.
Linux filesystems reserve a small percentage of space for root to prevent full disk errors. df shows available space for normal users, which excludes reserved blocks.
Result
You understand why df might show less free space than expected and why root can still write when others cannot.
Knowing reserved space prevents confusion about disk full errors and system stability.
7
ExpertHow df calculates disk usage internally
🤔Before reading on: do you think df reads file sizes directly or queries the filesystem metadata? Commit to your answer.
Concept: Understand that df queries filesystem metadata, not individual files, for fast disk usage reporting.
df uses system calls to get filesystem statistics like total blocks, free blocks, and block size. It does not scan files, so it is fast even on large disks.
Result
You realize df is efficient and why it might differ from du, which scans files.
Understanding df's internal method explains its speed and limitations compared to other tools.
Under the Hood
df uses the statfs or statvfs system calls to ask the operating system for filesystem statistics. These calls return data like total blocks, free blocks, and block size. df then calculates used and available space by multiplying blocks by block size. It does not read individual files, so it is very fast. The output reflects the current state of the filesystem as reported by the kernel.
Why designed this way?
df was designed to provide a quick snapshot of disk usage without scanning files, which would be slow on large filesystems. Using system calls to get metadata is efficient and reliable. This design balances speed and accuracy for everyday monitoring needs. Alternatives like scanning files (du) exist but serve different purposes.
┌───────────────┐
│ df command    │
└──────┬────────┘
       │ calls statfs/statvfs
       ▼
┌───────────────┐
│ Kernel FS info│
│ (blocks, size)│
└──────┬────────┘
       │ returns stats
       ▼
┌───────────────┐
│ df calculates │
│ used/avail    │
└──────┬────────┘
       │ displays output
       ▼
┌───────────────┐
│ User sees disk│
│ usage summary │
└───────────────┘
Myth Busters - 4 Common Misconceptions
Quick: Does df show the exact disk space used by files or just filesystem metadata? Commit to yes or no.
Common Belief:df scans all files and sums their sizes to show disk usage.
Tap to reveal reality
Reality:df reads filesystem metadata about blocks and sizes, not individual files.
Why it matters:Believing df scans files leads to confusion when df and du outputs differ, causing misinterpretation of disk usage.
Quick: Does df show all free space available to every user? Commit to yes or no.
Common Belief:df shows all free space on the disk available to all users.
Tap to reveal reality
Reality:df excludes reserved space set aside for root, so normal users see less free space.
Why it matters:Not knowing about reserved space can cause surprise when users get disk full errors even though df shows free space.
Quick: Does df report space for unmounted or hidden filesystems? Commit to yes or no.
Common Belief:df shows disk usage for all disks and partitions, mounted or not.
Tap to reveal reality
Reality:df only reports on mounted filesystems visible in the directory tree.
Why it matters:Assuming df shows all disks can cause missed monitoring of unmounted or hidden partitions.
Quick: Can df show disk usage for network filesystems like NFS? Commit to yes or no.
Common Belief:df only works for local disks, not network mounts.
Tap to reveal reality
Reality:df works for all mounted filesystems, including network filesystems like NFS or SMB.
Why it matters:Knowing df works on network mounts helps monitor remote storage usage consistently.
Expert Zone
1
df output can differ depending on block size used internally; using -h or -B options controls this for clarity.
2
Some filesystems support compression or snapshots, which can make df's reported usage differ from actual file sizes.
3
When multiple mount points share the same device, df shows usage per mount, which can be confusing without understanding bind mounts or overlays.
When NOT to use
df is not suitable when you need detailed folder or file size information; use du instead. Also, df does not detect disk errors or fragmentation; use tools like smartctl or fsck for health checks.
Production Patterns
System administrators use df in scripts to monitor disk space and trigger alerts when free space is low. It is combined with cron jobs for regular checks and integrated into monitoring tools like Nagios or Prometheus exporters.
Connections
du (disk usage)
complements df by showing disk usage at directory and file level, while df shows filesystem-level usage
Understanding both df and du helps manage disk space from overall system to individual files.
Filesystem metadata
df relies on filesystem metadata to report usage without scanning files
Knowing how filesystems store metadata clarifies why df is fast and how it can differ from actual file sizes.
Inventory management
both track available resources and usage to prevent shortages
Just like a store tracks stock levels to avoid running out, df tracks disk space to prevent system failures.
Common Pitfalls
#1Misreading df output as exact file sizes
Wrong approach:df Filesystem 1024-blocks Used Available Use% Mounted on /dev/sda1 20971520 10485760 10485760 50% / # Assuming Used means sum of all file sizes exactly
Correct approach:df -h Filesystem Size Used Avail Use% Mounted on /dev/sda1 20G 10G 10G 50% / # Understand this is metadata-based usage, not file-by-file sum
Root cause:Confusing filesystem block usage with actual file sizes leads to wrong assumptions about disk usage.
#2Ignoring reserved space for root
Wrong approach:df -h Filesystem Size Used Avail Use% Mounted on /dev/sda1 20G 19G 1G 95% / # User tries to write 2G more and gets disk full error
Correct approach:Understand reserved blocks exist; root can write when normal users cannot.
Root cause:Not knowing reserved space causes surprise disk full errors despite apparent free space.
#3Running df on unmounted partitions expecting output
Wrong approach:df /dev/sdb1 # No output or error because /dev/sdb1 is not mounted
Correct approach:mount /dev/sdb1 /mnt df /mnt # Now df shows usage for that filesystem
Root cause:df only reports mounted filesystems; unmounted devices are invisible.
Key Takeaways
df is a quick tool to check disk space usage and availability on mounted filesystems.
It reports usage based on filesystem metadata, not by scanning files, making it fast but sometimes different from file sizes.
Reserved space for root users means normal users see less free space than total disk space.
Using options like -h and -T makes df output easier to read and understand.
df is essential for monitoring disk health and preventing storage-related system problems.

Practice

(1/5)
1. What does the df command do in Linux?
easy
A. Shows disk free space on mounted filesystems
B. Displays running processes
C. Lists files in a directory
D. Shows network connections

Solution

  1. Step 1: Understand the purpose of df

    The df command is used to report disk space usage on mounted filesystems.
  2. Step 2: Compare with other commands

    Other options like listing processes or files are done by different commands like ps or ls.
  3. Final Answer:

    Shows disk free space on mounted filesystems -> Option A
  4. Quick Check:

    df = disk free space [OK]
Hint: Remember: df = disk free space quick check [OK]
Common Mistakes:
  • Confusing df with ls or ps commands
  • Thinking df shows network info
  • Assuming df lists files
2. Which of these is the correct syntax to show disk space in human-readable format?
easy
A. df -r
B. df -h
C. df --list
D. df -x

Solution

  1. Step 1: Identify the option for human-readable sizes

    The -h option in df shows sizes in easy units like MB, GB.
  2. Step 2: Check other options

    Options like -r, --list, or -x do not provide human-readable output.
  3. Final Answer:

    df -h -> Option B
  4. Quick Check:

    -h means human-readable [OK]
Hint: Use -h for human-readable sizes with df [OK]
Common Mistakes:
  • Using -r or -x instead of -h
  • Forgetting the dash before h
  • Trying --list which is invalid
3. What will be the output of the command df -h /home?
medium
A. Shows disk usage of the /home filesystem in human-readable format
B. Shows disk usage of the root filesystem
C. Shows all mounted filesystems in bytes
D. Shows network disk usage

Solution

  1. Step 1: Understand the command arguments

    The command df -h /home checks disk space for the /home directory only, with human-readable sizes.
  2. Step 2: Eliminate other options

    It does not show root filesystem unless /home is on root, nor does it show all filesystems or network usage.
  3. Final Answer:

    Shows disk usage of the /home filesystem in human-readable format -> Option A
  4. Quick Check:

    df -h /home = /home usage human-readable [OK]
Hint: Add path after df -h to check specific folder space [OK]
Common Mistakes:
  • Assuming it shows all filesystems
  • Confusing /home with root filesystem
  • Expecting output in bytes without -h
4. You run df -h /nonexistent and get an error. What is the likely cause?
medium
A. You need root permissions to run df
B. The df command does not support -h option
C. The path /nonexistent does not exist or is not mounted
D. The disk is full

Solution

  1. Step 1: Analyze the error cause

    Running df on a path that does not exist or is not mounted causes an error.
  2. Step 2: Check other options

    df supports -h, does not require root, and disk full does not cause this error.
  3. Final Answer:

    The path /nonexistent does not exist or is not mounted -> Option C
  4. Quick Check:

    Invalid path = error [OK]
Hint: Check if path exists before using df on it [OK]
Common Mistakes:
  • Thinking -h is unsupported
  • Assuming root needed for df
  • Confusing disk full with path error
5. You want to check disk space for all mounted filesystems but exclude the tmpfs type. Which command will do this?
hard
A. df -h --exclude tmpfs
B. df -h --exclude=tmpfs
C. df -h --ignore-type tmpfs
D. df -h -x tmpfs

Solution

  1. Step 1: Identify the correct option to exclude filesystem type

    The -x option excludes a filesystem type in df. So df -h -x tmpfs excludes tmpfs.
  2. Step 2: Verify other options

    Options like --exclude-type, --exclude, or --ignore-type are invalid for df.
  3. Final Answer:

    df -h -x tmpfs -> Option D
  4. Quick Check:

    -x excludes filesystem type [OK]
Hint: Use -x to exclude filesystem types in df [OK]
Common Mistakes:
  • Using invalid options like --exclude-type
  • Confusing exclude with ignore
  • Forgetting dash before x
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard