Check Mounted File System Usage

LinuxLinuxIntermediate
Practice Now

Introduction

In this challenge, we will write a script to determine whether a given file system or mount point is mounted. We will use the df command to check if the file system is mounted or not. If it's mounted, we will print the free space available in it, otherwise, we will print an error message.


Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL shell(("`Shell`")) -.-> shell/ControlFlowGroup(["`Control Flow`"]) linux(("`Linux`")) -.-> linux/BasicSystemCommandsGroup(["`Basic System Commands`"]) linux(("`Linux`")) -.-> linux/InputandOutputRedirectionGroup(["`Input and Output Redirection`"]) linux(("`Linux`")) -.-> linux/SystemInformationandMonitoringGroup(["`System Information and Monitoring`"]) shell(("`Shell`")) -.-> shell/BasicSyntaxandStructureGroup(["`Basic Syntax and Structure`"]) shell(("`Shell`")) -.-> shell/VariableHandlingGroup(["`Variable Handling`"]) shell(("`Shell`")) -.-> shell/AdvancedScriptingConceptsGroup(["`Advanced Scripting Concepts`"]) shell(("`Shell`")) -.-> shell/SystemInteractionandConfigurationGroup(["`System Interaction and Configuration`"]) shell/ControlFlowGroup -.-> shell/if_else("`If-Else Statements`") linux/BasicSystemCommandsGroup -.-> linux/source("`Script Executing`") linux/BasicSystemCommandsGroup -.-> linux/exit("`Shell Exiting`") linux/BasicSystemCommandsGroup -.-> linux/echo("`Text Display`") linux/InputandOutputRedirectionGroup -.-> linux/redirect("`I/O Redirecting`") linux/SystemInformationandMonitoringGroup -.-> linux/free("`Memory Reporting`") linux/SystemInformationandMonitoringGroup -.-> linux/df("`Disk Space Reporting`") linux/SystemInformationandMonitoringGroup -.-> linux/mount("`File System Mounting`") shell/BasicSyntaxandStructureGroup -.-> shell/shebang("`Shebang`") shell/BasicSyntaxandStructureGroup -.-> shell/comments("`Comments`") shell/BasicSyntaxandStructureGroup -.-> shell/quoting("`Quoting Mechanisms`") shell/VariableHandlingGroup -.-> shell/variables_decl("`Variable Declaration`") shell/VariableHandlingGroup -.-> shell/variables_usage("`Variable Usage`") shell/VariableHandlingGroup -.-> shell/str_manipulation("`String Manipulation`") shell/VariableHandlingGroup -.-> shell/arrays("`Arrays`") shell/VariableHandlingGroup -.-> shell/param_expansion("`Parameter Expansion`") shell/ControlFlowGroup -.-> shell/for_loops("`For Loops`") shell/ControlFlowGroup -.-> shell/cond_expr("`Conditional Expressions`") shell/ControlFlowGroup -.-> shell/exit_status("`Exit and Return Status`") shell/AdvancedScriptingConceptsGroup -.-> shell/arith_expansion("`Arithmetic Expansion`") shell/AdvancedScriptingConceptsGroup -.-> shell/cmd_substitution("`Command Substitution`") shell/AdvancedScriptingConceptsGroup -.-> shell/subshells("`Subshells and Command Groups`") shell/SystemInteractionandConfigurationGroup -.-> shell/globbing_expansion("`Globbing and Pathname Expansion`") subgraph Lab Skills shell/if_else -.-> lab-18275{{"`Check Mounted File System Usage`"}} linux/source -.-> lab-18275{{"`Check Mounted File System Usage`"}} linux/exit -.-> lab-18275{{"`Check Mounted File System Usage`"}} linux/echo -.-> lab-18275{{"`Check Mounted File System Usage`"}} linux/redirect -.-> lab-18275{{"`Check Mounted File System Usage`"}} linux/free -.-> lab-18275{{"`Check Mounted File System Usage`"}} linux/df -.-> lab-18275{{"`Check Mounted File System Usage`"}} linux/mount -.-> lab-18275{{"`Check Mounted File System Usage`"}} shell/shebang -.-> lab-18275{{"`Check Mounted File System Usage`"}} shell/comments -.-> lab-18275{{"`Check Mounted File System Usage`"}} shell/quoting -.-> lab-18275{{"`Check Mounted File System Usage`"}} shell/variables_decl -.-> lab-18275{{"`Check Mounted File System Usage`"}} shell/variables_usage -.-> lab-18275{{"`Check Mounted File System Usage`"}} shell/str_manipulation -.-> lab-18275{{"`Check Mounted File System Usage`"}} shell/arrays -.-> lab-18275{{"`Check Mounted File System Usage`"}} shell/param_expansion -.-> lab-18275{{"`Check Mounted File System Usage`"}} shell/for_loops -.-> lab-18275{{"`Check Mounted File System Usage`"}} shell/cond_expr -.-> lab-18275{{"`Check Mounted File System Usage`"}} shell/exit_status -.-> lab-18275{{"`Check Mounted File System Usage`"}} shell/arith_expansion -.-> lab-18275{{"`Check Mounted File System Usage`"}} shell/cmd_substitution -.-> lab-18275{{"`Check Mounted File System Usage`"}} shell/subshells -.-> lab-18275{{"`Check Mounted File System Usage`"}} shell/globbing_expansion -.-> lab-18275{{"`Check Mounted File System Usage`"}} end

Mount Challenge

In this challenge, you are required to write a script to determine whether a given file system or mount point is mounted. You will need to check if the file system is mounted, print the free space available if it is, and provide an error message if it is not.

Tasks

You need to complete the following task:

  1. Check if the given file system is mounted or not.
  2. If it's mounted, print the free space available in it.
  3. Otherwise, print an error message.

Requirements

You must meet the following requirements:

  • Must use commands df, tr, and cut.
  • Utilize arrays and loops.
  • The script should be named mount.sh.
  • The working directory should be ~/project.

Example

Sample Execution / Output:

labex:project/ $ bash mount.sh /dev/vda3
Filesystem /dev/vda3 is 46% used with 21367676 KB free.

Note: /dev/vda3 is for example only. You should be able to use the available file system or mount point.

Summary

After completing this challenge, we will have a better understanding of mounting, file-systems, and device files. We will also learn how to use the df command to check if a file system is mounted or not, and how to print the free space available in it. Additionally, we will practice using arrays and loops in our script.

Other Linux Tutorials you may like