Bird
Raised Fist0
Linux CLIscripting~20 mins

du (disk usage by directory) in Linux CLI - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Disk Usage Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
💻 Command Output
intermediate
1:30remaining
What is the output of this du command?
You run the command du -sh /var/log on a Linux system. What does the output represent?
Linux CLI
du -sh /var/log
AThe total disk space used by the /var/log directory and all its contents, shown in a human-readable format.
BThe free disk space available on the partition containing /var/log.
CThe number of files inside /var/log directory.
DThe size of the largest file inside /var/log.
Attempts:
2 left
💡 Hint
Think about what the options -s and -h mean in the du command.
💻 Command Output
intermediate
1:30remaining
What does this command output?
You run du -h --max-depth=1 /home/user. What will the output show?
Linux CLI
du -h --max-depth=1 /home/user
AOnly the total disk usage of /home/user without details.
BDisk usage of all files inside /home/user recursively, one line per file.
CDisk usage of /home/user and each of its immediate subdirectories, in human-readable form.
DDisk usage of /home/user but excluding hidden files.
Attempts:
2 left
💡 Hint
The --max-depth=1 limits how deep du reports.
🔧 Debug
advanced
1:30remaining
Why does this du command fail?
You try to run du -h --maxdepth=1 /etc but get an error. What is the cause?
Linux CLI
du -h --maxdepth=1 /etc
AThe option should be spelled <code>--max-depth</code> with a hyphen, not <code>--maxdepth</code>.
BThe directory /etc does not exist.
CThe <code>-h</code> option cannot be combined with <code>--maxdepth</code>.
DYou need root permissions to run du on /etc.
Attempts:
2 left
💡 Hint
Check the spelling of the option carefully.
🚀 Application
advanced
2:00remaining
How to find the top 3 largest subdirectories in /var?
You want to list the three largest subdirectories inside /var by disk usage. Which command will do this?
Adu -sh /var/* | sort -h | tail -n 3
Bdu -h /var | sort -h | head -n 3
Cdu -sh /var | sort -hr | head -n 3
Ddu -h --max-depth=1 /var | sort -hr | head -n 3
Attempts:
2 left
💡 Hint
Use du with max-depth to get sizes of immediate subdirectories, then sort and pick top 3.
🧠 Conceptual
expert
2:00remaining
What is the effect of using du -b compared to du -h?
You run du -b /tmp and du -h /tmp. What is the difference in their outputs?
A<code>du -b</code> shows sizes in bits, <code>du -h</code> shows sizes in bytes.
B<code>du -b</code> shows disk usage in bytes (exact number), while <code>du -h</code> shows sizes in human-readable units like KB or MB.
C<code>du -b</code> shows sizes rounded to nearest KB, <code>du -h</code> shows exact bytes.
D<code>du -b</code> shows block counts, <code>du -h</code> shows bytes.
Attempts:
2 left
💡 Hint
Think about what the -b and -h options mean for size units.

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