📖 Scenario: You are managing a Linux server and want to check how much disk space is free on your system. This helps you avoid running out of space and keep your server running smoothly.
🎯 Goal: Learn to use the df command to display disk free space information and filter the output to see only the root filesystem usage.
📋 What You'll Learn
Use the df command to show disk space usage
Use a variable to store the filesystem name /
Use grep to filter the output for the root filesystem
Print the filtered disk space information
💡 Why This Matters
🌍 Real World
System administrators often need to monitor disk space to prevent servers from running out of space, which can cause crashes or data loss.
💼 Career
Knowing how to use basic Linux commands like <code>df</code> and scripting with variables and conditionals is essential for roles in system administration, DevOps, and IT support.
Progress0 / 4 steps
1
Create a variable for the root filesystem
Create a variable called root_fs and set it to the string "/" to represent the root filesystem.
Linux CLI
Hint
Use root_fs="/" to store the root filesystem path.
2
Run the df command and filter for root filesystem
Use the df -h command piped to grep with the variable root_fs to filter the output and show only the disk space info for the root filesystem. Store this output in a variable called root_usage.
Linux CLI
Hint
Use root_usage=$(df -h | grep " $root_fs$") to get the filtered disk space info.
3
Print the root filesystem disk usage
Print the content of the variable root_usage to display the disk free space information for the root filesystem.
Linux CLI
Hint
Use echo "$root_usage" to print the disk usage info.
4
Add a check to show a message if root filesystem info is missing
Add an if statement to check if root_usage is empty. If it is empty, print "Root filesystem info not found.". Otherwise, print the root_usage variable.
Linux CLI
Hint
Use if [ -z "$root_usage" ] to check if the variable is empty.
Practice
(1/5)
1. What does the df command do in Linux?
easy
A. Shows disk free space on mounted filesystems
B. Displays running processes
C. Lists files in a directory
D. Shows network connections
Solution
Step 1: Understand the purpose of df
The df command is used to report disk space usage on mounted filesystems.
Step 2: Compare with other commands
Other options like listing processes or files are done by different commands like ps or ls.
Final Answer:
Shows disk free space on mounted filesystems -> Option A
Quick Check:
df = disk free space [OK]
Hint: Remember: df = disk free space quick check [OK]
Common Mistakes:
Confusing df with ls or ps commands
Thinking df shows network info
Assuming df lists files
2. Which of these is the correct syntax to show disk space in human-readable format?
easy
A. df -r
B. df -h
C. df --list
D. df -x
Solution
Step 1: Identify the option for human-readable sizes
The -h option in df shows sizes in easy units like MB, GB.
Step 2: Check other options
Options like -r, --list, or -x do not provide human-readable output.
Final Answer:
df -h -> Option B
Quick Check:
-h means human-readable [OK]
Hint: Use -h for human-readable sizes with df [OK]
Common Mistakes:
Using -r or -x instead of -h
Forgetting the dash before h
Trying --list which is invalid
3. What will be the output of the command df -h /home?
medium
A. Shows disk usage of the /home filesystem in human-readable format
B. Shows disk usage of the root filesystem
C. Shows all mounted filesystems in bytes
D. Shows network disk usage
Solution
Step 1: Understand the command arguments
The command df -h /home checks disk space for the /home directory only, with human-readable sizes.
Step 2: Eliminate other options
It does not show root filesystem unless /home is on root, nor does it show all filesystems or network usage.
Final Answer:
Shows disk usage of the /home filesystem in human-readable format -> Option A
Quick Check:
df -h /home = /home usage human-readable [OK]
Hint: Add path after df -h to check specific folder space [OK]
Common Mistakes:
Assuming it shows all filesystems
Confusing /home with root filesystem
Expecting output in bytes without -h
4. You run df -h /nonexistent and get an error. What is the likely cause?
medium
A. You need root permissions to run df
B. The df command does not support -h option
C. The path /nonexistent does not exist or is not mounted
D. The disk is full
Solution
Step 1: Analyze the error cause
Running df on a path that does not exist or is not mounted causes an error.
Step 2: Check other options
df supports -h, does not require root, and disk full does not cause this error.
Final Answer:
The path /nonexistent does not exist or is not mounted -> Option C
Quick Check:
Invalid path = error [OK]
Hint: Check if path exists before using df on it [OK]
Common Mistakes:
Thinking -h is unsupported
Assuming root needed for df
Confusing disk full with path error
5. You want to check disk space for all mounted filesystems but exclude the tmpfs type. Which command will do this?
hard
A. df -h --exclude tmpfs
B. df -h --exclude=tmpfs
C. df -h --ignore-type tmpfs
D. df -h -x tmpfs
Solution
Step 1: Identify the correct option to exclude filesystem type
The -x option excludes a filesystem type in df. So df -h -x tmpfs excludes tmpfs.
Step 2: Verify other options
Options like --exclude-type, --exclude, or --ignore-type are invalid for df.
Final Answer:
df -h -x tmpfs -> Option D
Quick Check:
-x excludes filesystem type [OK]
Hint: Use -x to exclude filesystem types in df [OK]
Common Mistakes:
Using invalid options like --exclude-type
Confusing exclude with ignore
Forgetting dash before x
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.