Bird
Raised Fist0
Linux CLIscripting~10 mins

du (disk usage by directory) in Linux CLI - Step-by-Step Execution

Choose your learning style10 modes available
Concept Flow - du (disk usage by directory)
Start du command
↓
Read directory contents
↓
Calculate size of each file
↓
Sum sizes per directory
↓
Display sizes
↓
End
The du command reads directory contents, calculates file sizes, sums them per directory, and displays the disk usage.
Execution Sample
Linux CLI
du -sh /home/user/Documents
Shows the total disk usage of the Documents directory in a human-readable format.
Execution Table
StepActionDirectory/FileSize CalculatedOutput
1Start du command/home/user/Documents
2Read directory contents/home/user/Documents
3Calculate size/home/user/Documents/file1.txt4K
4Calculate size/home/user/Documents/file2.pdf12M
5Read directory contents/home/user/Documents/Subfolder
6Calculate size/home/user/Documents/Subfolder/file3.jpg2M
7Sum sizes/home/user/Documents/Subfolder2M
8Sum sizes/home/user/Documents14M
9Display total size/home/user/Documents14M14M /home/user/Documents
💡 All files and subdirectories processed, total size displayed.
Variable Tracker
VariableStartAfter Step 3After Step 4After Step 6After Step 7After Step 8Final
Current File/Dir/home/user/Documents/file1.txt/home/user/Documents/file2.pdf/home/user/Documents/Subfolder/file3.jpg/home/user/Documents/Subfolder/home/user/Documents
Size Calculated4K12M2M2M14M14M
Key Moments - 2 Insights
Why does du show sizes for subdirectories separately before summing?
du calculates sizes for each file and subdirectory first (see steps 3-6), then sums them up (steps 7-8) to show total usage.
What does the -h option do in du -sh?
The -h option makes sizes human-readable (like 4K, 12M) instead of bytes, making it easier to understand the output (step 9).
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what size is calculated for /home/user/Documents/file2.pdf at step 4?
A12M
B4K
C2M
D14M
💡 Hint
Check the 'Size Calculated' column at step 4 in the execution_table.
At which step does du sum the sizes of the Subfolder directory?
AStep 5
BStep 6
CStep 7
DStep 8
💡 Hint
Look for the 'Sum sizes' action related to Subfolder in the execution_table.
If the -h option was removed, how would the output at step 9 change?
ASizes would be shown in megabytes only
BSizes would be shown in bytes instead of human-readable units
COutput would include file names only
DOutput would be sorted alphabetically
💡 Hint
The -h option controls human-readable size formatting as seen in the execution_sample description.
Concept Snapshot
du command shows disk usage of directories.
Syntax: du [options] [directory]
-h shows sizes in human-readable form.
-s shows total size only.
Reads files, sums sizes, displays result.
Full Transcript
The du command calculates disk usage by reading directory contents, measuring each file's size, summing sizes for subdirectories, and then displaying the total size. Using options like -h makes the output easier to read by showing sizes in KB, MB, etc. The process starts by reading files, calculating sizes step-by-step, summing them, and finally printing the total usage. This helps users understand how much space a directory and its contents occupy on disk.

Practice

(1/5)
1. What does the du command primarily show in Linux?
easy
A. The disk space used by files and directories
B. The number of files in a directory
C. The current directory path
D. The list of running processes

Solution

  1. Step 1: Understand the purpose of du

    The du command stands for disk usage and shows how much space files and directories use.
  2. Step 2: Compare options

    Options B, C, and D relate to other commands like ls or ps, not du.
  3. Final Answer:

    The disk space used by files and directories -> Option A
  4. Quick Check:

    du = disk space used [OK]
Hint: Remember: du = disk usage, shows space used [OK]
Common Mistakes:
  • Confusing du with ls or ps commands
  • Thinking du shows file counts
  • Assuming du shows current directory path
2. Which du command option shows sizes in a human-readable format?
easy
A. -s
B. -c
C. -a
D. -h

Solution

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

    The -h option makes sizes easier to read by adding units like K, M, G.
  2. Step 2: Understand other options

    -s summarizes, -a shows all files, -c adds a total line.
  3. Final Answer:

    -h -> Option D
  4. Quick Check:

    -h = human-readable sizes [OK]
Hint: Use -h for sizes like KB, MB, GB [OK]
Common Mistakes:
  • Using -s expecting human-readable sizes
  • Confusing -a or -c with size formatting
  • Omitting the -h option for readability
3. What is the output of the command du -sh /var/log if the /var/log directory uses 120M of disk space?
medium
A. 120M
B. 120M /var/log
C. 120MB /var/log
D. 120 /var/log

Solution

  1. Step 1: Understand the command options

    -s summarizes total size, -h shows human-readable format.
  2. Step 2: Predict output format

    The output shows size with unit, a tab, then directory path: "120M\t/var/log".
  3. Final Answer:

    120M /var/log -> Option B
  4. Quick Check:

    du -sh output = size + tab + path [OK]
Hint: du -sh shows size and path separated by tab [OK]
Common Mistakes:
  • Omitting the tab separator
  • Writing size without unit
  • Showing size without path
4. You run du -h /home/user but get an error: "du: invalid option -- 'h'". What is the likely cause?
medium
A. The directory /home/user does not exist
B. You need to run the command as root
C. The du command does not support the -h option on this system
D. You must use --human-readable instead of -h

Solution

  1. Step 1: Analyze the error message

    The error says -h is invalid, meaning this du version lacks that option.
  2. Step 2: Check other options

    Running as root or directory existence wouldn't cause invalid option error; -h is standard, no need for long form.
  3. Final Answer:

    The du command does not support the -h option on this system -> Option C
  4. Quick Check:

    Invalid option error means unsupported flag [OK]
Hint: Invalid option means your du version lacks that flag [OK]
Common Mistakes:
  • Assuming permission or path errors cause invalid option
  • Trying --human-readable instead of checking version
  • Ignoring error message details
5. You want to find the total disk usage of all subdirectories inside /home/user but only see sizes for individual files. Which command correctly summarizes each subdirectory size in human-readable form?
hard
A. du -sh /home/user/*
B. du -ah /home/user
C. du -h /home/user
D. du -s /home/user

Solution

  1. Step 1: Understand the goal

    You want sizes of each subdirectory summarized, not individual files.
  2. Step 2: Analyze options

    du -ah shows all files and dirs, du -sh on directory alone summarizes total only, du -s no human-readable. du -sh /home/user/* summarizes each subdirectory separately in human-readable form.
  3. Final Answer:

    du -sh /home/user/* -> Option A
  4. Quick Check:

    Use du -sh with wildcard for each subdir [OK]
Hint: Use du -sh with * to summarize each subdirectory [OK]
Common Mistakes:
  • Using -a to include files instead of just dirs
  • Using -s alone for total only
  • Not using wildcard to list subdirectories
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