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
Menu Driven Shell Script to Check Memory and Disk Usages In Linux most of the time, we automate things using the bash scripts. Some script has only one functionality, but some script can do more than one functions. For that, we have to provide the menu to select the option which function should be performed now. This script is called as Menu-Driven Progra
4 min read
How to Check Disk Space in Linux Efficient disk space management is important for maintaining the performance and stability of your Linux system. Over time, files and applications can fill up your storage, potentially causing slowdowns or errors. Commands to Check DIsk Space in LinuxKnowing how to check disk space in Linux helps yo
6 min read
Shell Scripting to check System Performance Prerequisites: Bash Scripting, Shell Function Library We will build a bash script to check : memory utilizationCPU utilisationdisk utilisation And analyze the report and print messages or error warnings accordingly. Creating Shell Script The name of the script will be sysperform.sh. Make it an execu
8 min read
Pydf - Check disk space usage with colored output Pydf is a command-line-based tool that shows the amount of disk space available on the mounted file system. This tool is similar to the df command, but pydf command gives output in different colors for different file systems.pydf tool is highly customizable, and this tool is written in the python la
2 min read
Disk Cleanup Using Powershell Scripts In this article, we will look at how to delete temporary files, recycle bin data, and run a disc cleanup program in Windows. Windows users sometimes faced low disk space errors with the error "You're running out of space on this PC. Manage storage to view usage and free up some space." When Windows
5 min read