Introduction
This script uses the du
and df
commands built into Linux to analyze disk usage and generate visual data for the user to analyze. We’ll also make use of the gnuplot
tool to help visualize the data.
The script can be customized according to user requirements through command-line arguments, which we will cover in detail later in this document.
Please ensure you have the necessary permissions to run disk analysis on your system.
Prerequisites
- Familiarity with Bash scripting.
- A Linux or Unix-based system.
du
anddf
commands available.gnuplot
installed for visualizing data.
Script Structure
The script is broken down into several key sections:
- Parsing command line arguments
- Function definitions
- Main script logic
- Data visualization
1. Parsing Command Line Arguments
First, we’ll parse command-line arguments using getopts
. We’ll add options for the user to specify a directory (-d
), output file (-o
), and help (-h
).
while getopts "d:o:h" opt; do
case ${opt} in
d )
dir=$OPTARG
;;
o )
output=$OPTARG
;;
h )
echo "Usage: cmd [-h] [-d directory] [-o output_file]"
exit 0
;;
\? )
echo "Invalid Option: -$OPTARG" 1>&2
exit 1
;;
: )
echo "Invalid Option: -$OPTARG requires an argument" 1>&2
exit 1
;;
esac
done
shift $((OPTIND -1))
2. Function Definitions
Next, we’ll define two functions, analyze_disk_usage
and disk_usage_visual
.
analyze_disk_usage
will use du
and df
commands to analyze the disk usage:
function analyze_disk_usage {
echo "Analyzing disk usage for directory $1..."
du -sh $1 > $2
df -h >> $2
}
disk_usage_visual
will use gnuplot
to create a graphical representation of the disk usage:
function disk_usage_visual {
echo "Generating disk usage graph..."
gnuplot <<- EOF
set term png
set output "$1.png"
set title "Disk Usage"
set ylabel "Size"
set xlabel "Directory"
plot "$1" using 1:2 title "Disk usage" with boxes
EOF
}
3. Main Script Logic
Here’s where the script will call the defined functions based on the provided arguments. It’ll check if a directory and output file were provided, and if not, it’ll use default values.
dir=${dir:-/}
output=${output:-disk_usage_report.txt}
analyze_disk_usage $dir $output
disk_usage_visual $output
4. Data Visualization
Finally, after the script completes its execution, you can open the generated PNG image to view the graphical representation of disk usage.
Using the Script
To use the script, simply call it from the command line with desired options. Here are a few examples:
- Analyze the root directory and output to a file called
disk_report.txt
:bash disk_usage.sh -d / -o disk_report.txt
- Analyze the home directory with default output:
bash disk_usage.sh -d /home
- Display help:
bash disk_usage.sh -h
Conclusion
This script provides a basic framework for disk usage analysis and visualization. Depending on your needs, you may want to expand its functionality, such as adding more complex analysis, handling more command-line arguments, or refining the visualization.