-
Notifications
You must be signed in to change notification settings - Fork 18
/
Copy pathfdu.sh
executable file
·56 lines (53 loc) · 2.39 KB
/
fdu.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
#!/bin/bash
#Usage: ./fdu.sh
#Description: Script made to resolve Issue #14: Watching Free Disk Space
#Provided By Saumit Dinesan <[email protected]>
############################################################
# Help #
############################################################
Help()
{
# Display Help
echo "Usage: fd [OPTION]... [LOCATION]..."
echo "Easy Calculate Free Disk space!"
echo
echo "options: [-c|-d|-h|-v]"
echo "-c Current harddisk mountpoint"
echo "-d Specific disk location"
echo "-h Print this Help."
echo "-v Verbose mode (displays output of df command in human readable format)"
}
############################################################
############################################################
# Main program #
############################################################
############################################################
while getopts ":d:v:hc" option; do
case $option in
h) # display Help
Help
exit;;
c)
location=$(findmnt -T . | awk 'FNR ==2 {print $1}') && loc=$(basename "$location")
icon="🖴 "
printf "%s %s: %s\n" "Current disk mountpoint" "$icon" "$location"
printf "%s: %s\n" "The available free fisk space on $loc is" "$(df -h "$location" | awk '/[0-9]/ {print $4" ""("100-$5"%"")"}')"
;;
d)
location=$OPTARG
if [ -z "$location" ]; then echo "Enter disk location: " && read -r location ; fi
icon="🖴 "
if [[ "$location" =~ ^(/home|home)$ ]] ; then loc=home && location="/$loc"; fi
if [[ "$location" =~ ^(/|root)$ ]] ; then loc=root && location="/$loc"; fi
if [[ "$location" =~ ^(/mnt|mnt)$ ]] ; then loc=mnt && location="/$loc"; fi
if [[ "$location" =~ ^(.)$ ]] ; then location=$(findmnt -T . | awk 'FNR ==2 {print $1}') && loc=$(basename "$location");
else loc=$(basename "$location"); fi
printf "%s %s: %s\n" "$loc" "$icon" "$(df -h "$location" | awk ' /[0-9]/ {print $3 "/" $2}')"
printf "%s: %s\n" "Hence, the free fisk space on $loc is" "$(df -h "$location" | awk '/[0-9]/ {print $4" ""("100-$5"%"")"}')" ;;
v)
df -h;;
*) # Invalid option
echo "Error: Invalid option"
exit;;
esac
done