0
0
Linux CLIscripting~30 mins

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

Choose your learning style9 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
Need a 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
Need a 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
Need a 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
Need a hint?

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