0
0
Linux CLIscripting~10 mins

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

Choose your learning style9 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.