Bird
Raised Fist0
Linux CLIscripting~10 mins

du (disk usage by directory) in Linux CLI - Interactive Code Practice

Choose your learning style10 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to show disk usage of the current directory.

Linux CLI
du [1] .
Drag options to blanks, or click blank then click option'
A-h
B-a
C-s
D-x
Attempts:
3 left
💡 Hint
Common Mistakes
Using -s only shows total size, not all directories.
Using -a shows all files, which is more than needed.
2fill in blank
medium

Complete the code to show only the total disk usage of the current directory.

Linux CLI
du [1] .
Drag options to blanks, or click blank then click option'
A-a
B-h
C-s
D-c
Attempts:
3 left
💡 Hint
Common Mistakes
Using -a lists all files and directories, too much detail.
Using -c adds a grand total, not just the directory total.
3fill in blank
hard

Fix the error in the code to show disk usage in human-readable format for /var directory.

Linux CLI
du -h [1]
Drag options to blanks, or click blank then click option'
A/usr/var
Bvar
C~/var
D/var
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'var' without slash looks in current directory, which may not exist.
Using '~/var' looks in home directory, not /var.
4fill in blank
hard

Fill both blanks to show disk usage of /home directory in human-readable format and summarize only total size.

Linux CLI
du [1] [2] /home
Drag options to blanks, or click blank then click option'
A-h
B-s
C-a
D-c
Attempts:
3 left
💡 Hint
Common Mistakes
Using -a shows all files, not just summary.
Using -c adds a grand total which is not needed here.
5fill in blank
hard

Fill all three blanks to create a dictionary comprehension that maps each subdirectory in /tmp to its size in human-readable format, only if size is greater than 100K.

Linux CLI
sizes = [1]: subprocess.check_output(['du', '-h', '-s', f'/tmp/[2]']).decode().split()[0] for [3] in os.listdir('/tmp') if int(subprocess.check_output(['du', '-s', f'/tmp/[2]']).decode().split()[0]) > 100}
Drag options to blanks, or click blank then click option'
Ad
Ditem
Attempts:
3 left
💡 Hint
Common Mistakes
Using different variable names causes errors.
Using 'item' instead of 'd' breaks the comprehension.

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