du (disk usage by directory) in Linux CLI - Time & Space Complexity
When using the du command, it is important to understand how its execution time changes as the number of files and directories grows.
We want to know how the command's work increases when the directory size increases.
Analyze the time complexity of the following du command usage.
du -sh /path/to/directory
This command calculates the total disk usage of the specified directory and all its contents.
Look for repeated actions inside the command's process.
- Primary operation: Traversing each file and subdirectory inside the target directory.
- How many times: Once for every file and folder found inside the directory tree.
The command must check every file and folder to calculate sizes, so the work grows as the number of items grows.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 files/folders | About 10 checks |
| 100 files/folders | About 100 checks |
| 1000 files/folders | About 1000 checks |
Pattern observation: The number of operations grows roughly in direct proportion to the number of files and folders.
Time Complexity: O(n)
This means the time taken grows linearly with the number of files and directories inside the target folder.
[X] Wrong: "The du command only checks the top-level directory, so time stays the same no matter how many files are inside."
[OK] Correct: du actually looks inside every subdirectory and file to sum sizes, so more files mean more work.
Understanding how commands like du scale helps you reason about performance in real systems and scripts, a useful skill in many automation tasks.
"What if we use du with the --max-depth=1 option? How would the time complexity change?"
Practice
du command primarily show in Linux?Solution
Step 1: Understand the purpose of
Theduducommand stands for disk usage and shows how much space files and directories use.Step 2: Compare options
Options B, C, and D relate to other commands likelsorps, notdu.Final Answer:
The disk space used by files and directories -> Option AQuick Check:
du= disk space used [OK]
- Confusing du with ls or ps commands
- Thinking du shows file counts
- Assuming du shows current directory path
du command option shows sizes in a human-readable format?Solution
Step 1: Identify the option for human-readable sizes
The-hoption makes sizes easier to read by adding units like K, M, G.Step 2: Understand other options
-ssummarizes,-ashows all files,-cadds a total line.Final Answer:
-h -> Option DQuick Check:
-h= human-readable sizes [OK]
- Using -s expecting human-readable sizes
- Confusing -a or -c with size formatting
- Omitting the -h option for readability
du -sh /var/log if the /var/log directory uses 120M of disk space?Solution
Step 1: Understand the command options
-ssummarizes total size,-hshows human-readable format.Step 2: Predict output format
The output shows size with unit, a tab, then directory path: "120M\t/var/log".Final Answer:
120M /var/log -> Option BQuick Check:
du -sh output = size + tab + path [OK]
- Omitting the tab separator
- Writing size without unit
- Showing size without path
du -h /home/user but get an error: "du: invalid option -- 'h'". What is the likely cause?Solution
Step 1: Analyze the error message
The error says-his invalid, meaning thisduversion lacks that option.Step 2: Check other options
Running as root or directory existence wouldn't cause invalid option error;-his standard, no need for long form.Final Answer:
Theducommand does not support the-hoption on this system -> Option CQuick Check:
Invalid option error means unsupported flag [OK]
- Assuming permission or path errors cause invalid option
- Trying --human-readable instead of checking version
- Ignoring error message details
/home/user but only see sizes for individual files. Which command correctly summarizes each subdirectory size in human-readable form?Solution
Step 1: Understand the goal
You want sizes of each subdirectory summarized, not individual files.Step 2: Analyze options
du -ahshows all files and dirs,du -shon directory alone summarizes total only,du -sno human-readable.du -sh /home/user/*summarizes each subdirectory separately in human-readable form.Final Answer:
du -sh /home/user/* -> Option AQuick Check:
Use du -sh with wildcard for each subdir [OK]
- Using -a to include files instead of just dirs
- Using -s alone for total only
- Not using wildcard to list subdirectories
Ready to prove it?
Pass the test and this pattern moves unknown
