Bird
Raised Fist0
Linux CLIscripting~30 mins

du (disk usage by directory) in Linux CLI - Mini Project: Build & Apply

Choose your learning style10 modes available
Disk Usage Summary with du Command
📖 Scenario: You are managing files on your Linux system. You want to check how much disk space different directories use to keep your system organized and avoid running out of space.
🎯 Goal: Build a simple script that uses the du command to show disk usage of directories in a specified folder, filter results by a size threshold, and display the filtered output.
📋 What You'll Learn
Use the du command to get disk usage of directories
Create a variable to hold the target directory path
Create a variable to hold the minimum size threshold in kilobytes
Filter the du output to show only directories larger than the threshold
Print the filtered disk usage results
💡 Why This Matters
🌍 Real World
System administrators often need to monitor disk usage to prevent storage issues and optimize space.
💼 Career
Knowing how to use disk usage commands and filter their output is essential for Linux system management and troubleshooting.
Progress0 / 4 steps
1
Set the target directory
Create a variable called target_dir and set it to "/var/log" to specify the directory you want to check disk usage for.
Linux CLI
Hint

Use target_dir="/var/log" to set the directory path.

2
Set the minimum size threshold
Create a variable called min_size and set it to 1000 to represent the minimum directory size in kilobytes for filtering.
Linux CLI
Hint

Use min_size=1000 to set the size threshold in KB.

3
Get and filter disk usage
Use the du -k --max-depth=1 "$target_dir" command to get disk usage in kilobytes for directories inside target_dir. Then use awk to filter and keep only lines where the size is greater than or equal to min_size. Store the filtered output in a variable called filtered_usage.
Linux CLI
Hint

Use du -k --max-depth=1 "$target_dir" piped to awk -v min="$min_size" '$1 >= min'.

4
Display the filtered disk usage
Print the contents of the filtered_usage variable to show the directories that meet the size threshold.
Linux CLI
Hint

Use echo "$filtered_usage" to display the filtered results.

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