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?"