📖 Scenario: You are a system administrator responsible for keeping a Linux server running smoothly. One important task is to monitor disk usage to prevent the server from running out of space, which can cause outages and stop important services.
🎯 Goal: Build a simple script that checks disk usage on the root partition and alerts if the usage is above a certain threshold. This helps prevent outages by giving early warnings to free up space.
📋 What You'll Learn
Use the df command to get disk usage of the root partition
Create a variable to hold the disk usage percentage as a number
Set a threshold variable for maximum allowed disk usage
Use an if statement to compare usage with the threshold
Print a warning message if usage is above the threshold, otherwise print a safe message
💡 Why This Matters
🌍 Real World
System administrators monitor disk space to avoid server crashes and service interruptions caused by full disks.
💼 Career
Knowing how to automate disk usage checks and alerts is a key skill for Linux system administrators and DevOps engineers.
Progress0 / 4 steps
1
Get disk usage of root partition
Use the df command with -h and / to get disk usage of the root partition. Then create a variable called disk_usage that stores the usage percentage as a string (e.g., '75%'). Use df -h / | tail -1 | awk '{print $5}' to get this value.
Linux CLI
Hint
Use df -h / to get disk info for root, then extract the fifth column for usage.
2
Set a threshold for disk usage
Create a variable called threshold and set it to 80. This means if disk usage is above 80%, we want to get a warning.
Linux CLI
Hint
Just assign the number 80 to the variable threshold.
3
Compare disk usage with threshold
Remove the percent sign from disk_usage and convert it to a number called usage_num. Then use an if statement to check if usage_num is greater than threshold. If yes, set a variable alert_msg to "Warning: Disk usage is above threshold!". Otherwise, set alert_msg to "Disk usage is safe.".
Linux CLI
Hint
Use ${disk_usage%?} to remove the percent sign. Use if [ "$usage_num" -gt "$threshold" ] for comparison.
4
Print the alert message
Print the value of alert_msg to show the disk usage status.
Linux CLI
Hint
Use printf "%s\n" "$alert_msg" or echo "$alert_msg" to print.
Practice
(1/5)
1. Why is regular disk management important to prevent system outages?
easy
A. It helps avoid running out of disk space and system crashes.
B. It makes the computer run faster by increasing CPU speed.
C. It changes the screen resolution automatically.
D. It updates the operating system without user permission.
Solution
Step 1: Understand disk space impact
Running out of disk space can cause programs to fail and the system to crash.
Step 2: Recognize disk management role
Disk management monitors and frees space to keep the system stable.
Final Answer:
It helps avoid running out of disk space and system crashes. -> Option A
Quick Check:
Disk space management prevents crashes = B [OK]
Hint: Disk space full? System crashes. Manage disks to prevent that. [OK]
Common Mistakes:
Confusing disk management with CPU or display settings
Thinking disk management updates software automatically
Ignoring disk space as a cause of outages
2. Which Linux command correctly shows disk usage of all mounted filesystems?
easy
A. top
B. ls -l
C. df -h
D. ps aux
Solution
Step 1: Identify command purpose
The df command reports disk space usage; -h makes it human-readable.
Step 2: Compare other commands
ls -l lists files, top shows processes, ps aux lists running processes.
Final Answer:
df -h -> Option C
Quick Check:
Disk usage command = df -h [OK]
Hint: Use df -h to check disk space in human-friendly format. [OK]
Common Mistakes:
Using ls or ps commands to check disk space
Forgetting the -h option for readable sizes
Confusing process commands with disk commands
3. What is the output of the command lsblk on a Linux system?
medium
A. Prints the contents of the current directory.
B. Shows current CPU usage statistics.
C. Displays network interface details.
D. Lists all block devices like disks and partitions.
Solution
Step 1: Understand lsblk command
lsblk lists block devices such as hard drives and their partitions.
Step 2: Eliminate unrelated outputs
CPU stats, network info, and directory contents are shown by other commands like top, ifconfig, and ls.
Final Answer:
Lists all block devices like disks and partitions. -> Option D
Quick Check:
lsblk shows disks and partitions = A [OK]
Hint: lsblk lists disks and partitions clearly. [OK]
Common Mistakes:
Confusing lsblk with commands showing CPU or network info
Expecting file listings instead of device info
Assuming lsblk modifies disks instead of listing
4. You run df -h and see 100% usage on /dev/sda1. What is the best immediate action to prevent outages?
medium
A. Change the screen resolution to reduce load.
B. Delete unnecessary files or move data to free space.
C. Run top to kill CPU-heavy processes.
D. Restart the computer immediately without checking disk.
Solution
Step 1: Analyze disk full problem
100% disk usage means no free space, risking crashes and data loss.
Step 2: Choose proper action
Deleting or moving files frees space immediately; restarting or changing screen resolution won't fix disk space.
Final Answer:
Delete unnecessary files or move data to free space. -> Option B
Quick Check:
Free disk space to prevent outages = D [OK]
Hint: Free disk space first to avoid crashes. [OK]
Common Mistakes:
Restarting without fixing disk space
Confusing CPU load with disk space issues
Trying unrelated fixes like screen changes
5. You want to automate disk checks to prevent outages. Which script snippet correctly checks disk usage and warns if usage exceeds 90% on /dev/sda1?
hard
A. usage=$(df /dev/sda1 | tail -1 | awk '{print $5}' | sed 's/%//'); if [ "$usage" -gt 90 ]; then echo "Warning: Disk usage above 90%"; fi
B. usage=$(top -b | grep sda1); if [ "$usage" -gt 90 ]; then echo "Warning"; fi
C. usage=$(lsblk /dev/sda1); if [ "$usage" -gt 90 ]; then echo "Warning"; fi
D. usage=$(ps aux | grep sda1); if [ "$usage" -gt 90 ]; then echo "Warning"; fi
Solution
Step 1: Extract disk usage percentage correctly
The command df /dev/sda1 shows disk usage; awk '{print $5}' gets the percent; sed 's/%//' removes the percent sign.
Step 2: Use conditional to warn if usage > 90%
The if statement compares usage as a number and prints a warning if above 90.
Final Answer:
usage=$(df /dev/sda1 | tail -1 | awk '{print $5}' | sed 's/%//'); if [ "$usage" -gt 90 ]; then echo "Warning: Disk usage above 90%"; fi -> Option A
Quick Check:
Correct disk usage check and warning = A [OK]
Hint: Use df and awk to get usage, then compare with if. [OK]
Common Mistakes:
Using unrelated commands like top or ps for disk usage
Not removing % sign before numeric comparison
Incorrect syntax in if statement
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.