What command in bash can you use to check disk space usage?
You can use the df command to check disk space usage in bash. It shows the amount of disk space used and available on filesystems.
Click to reveal answer
beginner
What does the -h option do when used with the df command?
The -h option makes the output human-readable by showing sizes in KB, MB, or GB instead of blocks.
Click to reveal answer
beginner
In a disk space monitoring script, why do we compare disk usage against a threshold?
We compare disk usage against a threshold to know when disk space is running low. This helps to alert or take action before the disk is full.
Click to reveal answer
intermediate
What bash command can be used to send an alert email from a disk space monitoring script?
You can use the mail or sendmail command to send an alert email from a bash script when disk space is low.
Click to reveal answer
intermediate
How can you schedule a disk space monitoring script to run automatically every day?
You can schedule the script using cron by adding a cron job that runs the script daily at a set time.
Click to reveal answer
Which command shows disk space usage in a human-readable format?
Als -l
Bcat /etc/passwd
Cdf -h
Dps aux
✗ Incorrect
The df -h command displays disk space usage with sizes in KB, MB, or GB.
What is a common threshold percentage to alert for low disk space?
A10%
B80%
C50%
D100%
✗ Incorrect
80% disk usage is a common threshold to alert administrators before disk space runs critically low.
Which bash command can be used to extract just the disk usage percentage from df output?
Aecho
Bgrep
Csed
Dawk
✗ Incorrect
awk is commonly used to parse and extract specific columns like disk usage percentage from df output.
How do you schedule a script to run every day at 2 AM using cron?
A0 2 * * * /path/to/script.sh
B2 0 * * * /path/to/script.sh
C* 2 * * * /path/to/script.sh
D0 0 2 * * /path/to/script.sh
✗ Incorrect
The cron expression "0 2 * * *" runs the script at 2:00 AM every day.
What is the purpose of sending an email alert in a disk space monitoring script?
ATo notify when disk space is low
BTo delete files automatically
CTo backup data
DTo increase disk space
✗ Incorrect
Email alerts notify administrators so they can take action before disk space runs out.
Describe how a basic disk space monitoring script works in bash.
Think about checking disk space, comparing it, and alerting.
You got /4 concepts.
Explain how to automate running a disk space monitoring script daily.
Automation uses cron jobs in Linux.
You got /4 concepts.
Practice
(1/5)
1. What is the main purpose of a disk space monitoring script in a server environment?
easy
A. To check disk usage and alert when space is low
B. To delete files automatically when disk is full
C. To speed up the disk read/write operations
D. To backup all files to another disk
Solution
Step 1: Understand the script's goal
The script's goal is to monitor disk usage to prevent running out of space.
Step 2: Identify the correct purpose
Among the options, only checking disk usage and alerting matches the monitoring function.
Final Answer:
To check disk usage and alert when space is low -> Option A
Quick Check:
Disk monitoring = alert on low space [OK]
Hint: Monitoring means checking and alerting, not deleting or backing up [OK]
Common Mistakes:
Confusing monitoring with automatic deletion
Thinking it speeds up disk operations
Assuming it performs backups
2. Which of the following is the correct syntax to get the disk usage percentage of the root partition using df and awk in bash?
easy
A. df -h / | awk '{print $5}'
B. df -h / | sed '2q;d'
C. df / | awk 'NR==1 {print $5}'
D. df -h / | awk 'NR==2 {print $5}'
Solution
Step 1: Understand df -h / output
The command shows disk usage for root; the second line (NR==2) has the data.
Step 2: Use awk 'NR==2 {print $5}' to get usage percent
This prints the 5th column (usage %) from the second line, which is correct.
Final Answer:
df -h / | awk 'NR==2 {print $5}' -> Option D
Quick Check:
Second line, fifth column = usage percent [OK]
Hint: Use NR==2 to skip header line in df output [OK]
Common Mistakes:
Printing all lines instead of second line
Using NR==1 which is header line
Using sed incorrectly to get the line
3. Given this script snippet:
usage=$(df / | awk 'NR==2 {print $5}' | sed 's/%//')
if [ "$usage" -gt 80 ]; then
echo "Warning: Disk usage is above 80%"
else
echo "Disk usage is normal"
fi
What will be the output if the disk usage is 75%?
medium
A. Disk usage is normal
B. No output
C. Syntax error in script
D. Warning: Disk usage is above 80%
Solution
Step 1: Extract usage value
The script removes % sign and stores 75 in usage variable.
Step 2: Compare usage with 80
75 is not greater than 80, so the else branch runs.
Final Answer:
Disk usage is normal -> Option A
Quick Check:
75 <= 80 means normal usage [OK]
Hint: Compare numeric values after removing % sign [OK]
Common Mistakes:
Forgetting to remove % sign before comparison
Using string comparison instead of numeric
Misreading the if condition
4. Identify the error in this disk space check script snippet:
usage=$(df / | awk 'NR==2 {print $5}')
if [ $usage -gt 90 ]; then
echo "Disk almost full"
fi
What problem will occur when running this script?
medium
A. No error, script runs fine
B. Syntax error due to missing quotes around $usage
C. df command will fail
D. awk command syntax error
Solution
Step 1: Check variable usage in test
The variable $usage contains a value like '85%'. Without quotes, test may fail if value has spaces or special chars.
Step 2: Identify missing % removal and quotes
Also, % sign is not removed, causing numeric comparison error. Missing quotes cause syntax error.
Final Answer:
Syntax error due to missing quotes around $usage -> Option B
Quick Check:
Always quote variables in test to avoid syntax errors [OK]
Hint: Quote variables and remove % before numeric comparison [OK]
Common Mistakes:
Not quoting variables in if test
Not removing % sign before numeric test
Assuming df or awk commands fail
5. You want to create a bash script that checks all mounted partitions and sends an alert if any partition exceeds 85% usage. Which approach correctly implements this?
hard
A. Use du command on root directory and alert if usage exceeds 85%
B. Run df -h once and alert if total disk usage exceeds 85%
C. Use a loop over df -h output lines, extract usage %, compare with 85, and echo alert if exceeded
D. Check only root partition usage and ignore others
Solution
Step 1: Understand requirement to check all partitions
We must check each mounted partition's usage, not just total or root.
Step 2: Loop through df -h output lines and compare usage
Extract usage percent per line, compare with 85, and alert if exceeded.
Final Answer:
Use a loop over df -h output lines, extract usage %, compare with 85, and echo alert if exceeded -> Option C
Quick Check:
Loop and compare each partition usage [OK]
Hint: Loop through df output lines to check each partition [OK]
Common Mistakes:
Checking only total or root partition
Using du instead of df for disk usage
Not looping through all partitions
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.