Bird
Raised Fist0
Linux CLIscripting~5 mins

du (disk usage by directory) in Linux CLI - Commands & Configuration

Choose your learning style10 modes available
Introduction
Sometimes your computer's storage gets full and you want to find out which folders use the most space. The du command helps you see how much disk space each directory and its contents take up.
When you want to check which folders are using the most space on your hard drive.
When you need to clean up space by deleting large unused directories.
When you want to monitor disk usage growth over time in specific folders.
When you are managing a server and want to avoid running out of disk space.
When you want a quick summary of disk usage for a directory and its subdirectories.
Commands
This command shows the disk usage of the /home/user/documents directory and all its subdirectories in a human-readable format (like KB, MB).
Terminal
du -h /home/user/documents
Expected OutputExpected
4.0K /home/user/documents/notes 12M /home/user/documents/photos 16M /home/user/documents
→
-h - Shows sizes in human-readable format (KB, MB, GB)
This command shows only the total disk usage of the /home/user/downloads directory in a human-readable format, without listing subdirectories.
Terminal
du -sh /home/user/downloads
Expected OutputExpected
250M /home/user/downloads
→
-s - Summarizes total size without showing subdirectories
→
-h - Shows sizes in human-readable format
This command shows disk usage for /var/log and its immediate subdirectories only, making it easier to see which subfolders use the most space.
Terminal
du -h --max-depth=1 /var/log
Expected OutputExpected
20M /var/log/apache2 5.0M /var/log/mysql 25M /var/log
→
--max-depth=1 - Limits output to the specified directory and its direct children
→
-h - Shows sizes in human-readable format
Key Concept

If you remember nothing else from this pattern, remember: du helps you find out how much space directories use so you can manage your disk storage better.

Common Mistakes
Running du without -h and seeing large numbers in bytes that are hard to read.
The output is difficult to understand because sizes are shown in bytes without units.
Always use the -h flag to get human-readable sizes like KB, MB, or GB.
Running du without -s and getting a long list of every subdirectory when only total size is needed.
The output becomes cluttered and hard to find the total size quickly.
Use the -s flag to get a summary total size for the directory.
Not using --max-depth and getting too much detail when only top-level folder sizes are needed.
The output is overwhelming and hard to interpret for large directory trees.
Use --max-depth=1 to limit output to the directory and its immediate subfolders.
Summary
Use du -h to see disk usage in a readable format for directories and subdirectories.
Use du -sh to get a quick total size of a directory without details.
Use du -h --max-depth=1 to see sizes of a directory and its immediate subfolders only.

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