Shell Script to Check Disk Space Usage

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.
Picked
Shell Script
Linux-Unix
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Docker - COPY Instruction
scp command in Linux with Examples
chown command in Linux with Examples
SED command in Linux | Set 2
Introduction to Linux Operating System
nohup Command in Linux with Examples
Array Basics in Shell Scripting | Set 1
mv command in Linux with examples
chmod command in Linux with examples
screen command in Linux with Examples