df (disk free space) in Linux CLI - Time & Space Complexity
We want to understand how the time taken by the df command changes as the number of mounted file systems grows.
How does the command's work increase when there are more disks or partitions?
Analyze the time complexity of this df command usage.
df -h
This command lists disk space usage for all mounted file systems in a human-readable format.
The command internally processes each mounted file system one by one.
- Primary operation: Reading and calculating disk usage for each mounted file system.
- How many times: Once per mounted file system.
As the number of mounted file systems increases, the command does more work linearly.
| Input Size (number of file systems) | Approx. Operations |
|---|---|
| 10 | About 10 disk usage checks |
| 100 | About 100 disk usage checks |
| 1000 | About 1000 disk usage checks |
Pattern observation: The work grows directly in proportion to the number of file systems.
Time Complexity: O(n)
This means the time taken grows in a straight line as the number of mounted file systems increases.
[X] Wrong: "The df command always runs instantly no matter how many disks there are."
[OK] Correct: The command must check each file system, so more disks mean more work and longer time.
Understanding how commands scale with input size helps you reason about system performance and scripting efficiency in real tasks.
"What if the command was run with a specific file system path instead of all? How would the time complexity change?"
Practice
df command do in Linux?Solution
Step 1: Understand the purpose of
Thedfdfcommand is used to report disk space usage on mounted filesystems.Step 2: Compare with other commands
Other options like listing processes or files are done by different commands likepsorls.Final Answer:
Shows disk free space on mounted filesystems -> Option AQuick Check:
df= disk free space [OK]
- Confusing df with ls or ps commands
- Thinking df shows network info
- Assuming df lists files
Solution
Step 1: Identify the option for human-readable sizes
The-hoption indfshows sizes in easy units like MB, GB.Step 2: Check other options
Options like-r,--list, or-xdo not provide human-readable output.Final Answer:
df -h -> Option BQuick Check:
-hmeans human-readable [OK]
- Using -r or -x instead of -h
- Forgetting the dash before h
- Trying --list which is invalid
df -h /home?Solution
Step 1: Understand the command arguments
The commanddf -h /homechecks disk space for the /home directory only, with human-readable sizes.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.Final Answer:
Shows disk usage of the /home filesystem in human-readable format -> Option AQuick Check:
df -h /home= /home usage human-readable [OK]
- Assuming it shows all filesystems
- Confusing /home with root filesystem
- Expecting output in bytes without -h
df -h /nonexistent and get an error. What is the likely cause?Solution
Step 1: Analyze the error cause
Runningdfon a path that does not exist or is not mounted causes an error.Step 2: Check other options
dfsupports-h, does not require root, and disk full does not cause this error.Final Answer:
The path /nonexistent does not exist or is not mounted -> Option CQuick Check:
Invalid path = error [OK]
- Thinking -h is unsupported
- Assuming root needed for df
- Confusing disk full with path error
tmpfs type. Which command will do this?Solution
Step 1: Identify the correct option to exclude filesystem type
The-xoption excludes a filesystem type indf. Sodf -h -x tmpfsexcludes tmpfs.Step 2: Verify other options
Options like--exclude-type,--exclude, or--ignore-typeare invalid fordf.Final Answer:
df -h -x tmpfs -> Option DQuick Check:
-xexcludes filesystem type [OK]
- Using invalid options like --exclude-type
- Confusing exclude with ignore
- Forgetting dash before x
Ready to prove it?
Pass the test and this pattern moves unknown
