Shell Script to Check Disk Space Usage
Last Updated :
20 Apr, 2021
Disk usage is a report generated by the Linux system about different disks available or created on the secondary memory. These disks are also known as partitions, they have their isolated filesystem. This facility provides us the measurements of different labels or features like Used space, Free space, Filesystem of the disk, etc. To see all these labels Linux has some internal command which helps us to visualize them, but they are terminal commands, and we need to create a shell script for the user using those commands.
Now we have to start the script by preparing an interface for the user, this interface will take input from the user for different types of Disk Usage report option given. This could be achieved by echo and read functionality present for the shell.
As for now, we have taken user input according to the available options present to the user. Now we have to prepare the further script for all the options present and compare that to the option's user asked for. We will use if-else conditions for this functionality. Linux CLI provides us a command to get Disk Usage, "df" has different options which help to retrieve particular features from the report. It has an "--output" option which can be used to print specific fields like: 'source', 'fstype', 'itotal', 'iused', 'iavail', 'ipcent', 'size', 'used', 'avail', 'pcent', 'file' and 'target'.
Code:
#!/bin/bash
echo -e "Select the Option From below:\n"
# -e option in echo command is used to
# enable interpretation of backslash escapes.
echo -e "\n
[ 1 ] For Only the Disk-Name and Used-Space \n
[ 2 ] For Only the Disk-Name and its Size \n
[ 3 ] To print Disk-Name and File-System \n
[ 4 ] To see all fields in DiskUsage \n"
# to take the user input
read userInput
# if to check the user input.
if [ $userInput == 1 ];
then
# -h is used for producing human readable and
# --output is used to specify field.
df -h --output = source,used
elif [ $userInput == 2 ];
then
df -h --output=source,size
elif [ $userInput == 3 ];
then
# "source" argument is for listing name of the source directory,
# "fstype" shows the file system type like ext4.
df -h --output=source,fstype
elif [ $userInput == 4 ];
then
# -a is used for all the fields.
df -ha
else
# if any wrong input is given.
echo "!!!!!!!!Wrong Output!!!!!!!!"
fi
Grant the executable permissions to the script from the terminal. This permission is given to the files to make them readable, writable, and most importantly executable for running them through the shell. Command "chmod" is used to give this kind of permissions, the "777" option stands for rwx(ReadWriteExecutable).
# chmod 777 DiskUsageScript.sh
Output:
If the input is 1. This only prints the disk name and the space used by that disk.
If Input is 4. This will print all available fields.
Similar Reads
Non-linear Components In electrical circuits, Non-linear Components are electronic devices that need an external power source to operate actively. Non-Linear Components are those that are changed with respect to the voltage and current. Elements that do not follow ohm's law are called Non-linear Components. Non-linear Co
11 min read
Spring Boot Tutorial Spring Boot is a Java framework that makes it easier to create and run Java applications. It simplifies the configuration and setup process, allowing developers to focus more on writing code for their applications. This Spring Boot Tutorial is a comprehensive guide that covers both basic and advance
10 min read
Class Diagram | Unified Modeling Language (UML) A UML class diagram is a visual tool that represents the structure of a system by showing its classes, attributes, methods, and the relationships between them. It helps everyone involved in a projectâlike developers and designersâunderstand how the system is organized and how its components interact
12 min read
Backpropagation in Neural Network Back Propagation is also known as "Backward Propagation of Errors" is a method used to train neural network . Its goal is to reduce the difference between the modelâs predicted output and the actual output by adjusting the weights and biases in the network.It works iteratively to adjust weights and
9 min read
3-Phase Inverter An inverter is a fundamental electrical device designed primarily for the conversion of direct current into alternating current . This versatile device , also known as a variable frequency drive , plays a vital role in a wide range of applications , including variable frequency drives and high power
13 min read
Polymorphism in Java Polymorphism in Java is one of the core concepts in object-oriented programming (OOP) that allows objects to behave differently based on their specific class type. The word polymorphism means having many forms, and it comes from the Greek words poly (many) and morph (forms), this means one entity ca
7 min read
Linux Commands Cheat Sheet Linux, often associated with being a complex operating system primarily used by developers, may not necessarily fit that description entirely. While it can initially appear challenging for beginners, once you immerse yourself in the Linux world, you may find it difficult to return to your previous W
13 min read
CTE in SQL In SQL, a Common Table Expression (CTE) is an essential tool for simplifying complex queries and making them more readable. By defining temporary result sets that can be referenced multiple times, a CTE in SQL allows developers to break down complicated logic into manageable parts. CTEs help with hi
6 min read
What is Vacuum Circuit Breaker? A vacuum circuit breaker is a type of breaker that utilizes a vacuum as the medium to extinguish electrical arcs. Within this circuit breaker, there is a vacuum interrupter that houses the stationary and mobile contacts in a permanently sealed enclosure. When the contacts are separated in a high vac
13 min read
Linux/Unix Tutorial Linux is one of the most widely used open-source operating systems. It's fast, secure, stable, and powers everything from smartphones and servers to cloud platforms and IoT devices. Linux is especially popular among developers, system administrators, and DevOps professionals.Linux is:A Unix-like OS
10 min read