Created
November 29, 2024 12:01
-
-
Save ostechnix/bf71da323e68367fddd5d0aa9f26d06f to your computer and use it in GitHub Desktop.
DiskInfo: A Bash Script to Gather Comprehensive Disk Information on Linux
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env bash | |
# ------------------------------------------------------------------ | |
# Script Name: DiskInfo | |
# Description: A Bash Script to gather comprehensive | |
# Disk information on Linux. | |
# Website: https://gist.github.com/ostechnix | |
# Version: 1.0 | |
# Usage: ./diskinfo.sh | |
# ------------------------------------------------------------------ | |
# Ensure `sudo` is authenticated at the beginning | |
echo "This script requires elevated privileges to run certain commands." | |
sudo -v || { echo "Error: You must provide sudo privileges to run this script."; exit 1; } | |
# Add `/sbin` and `/usr/sbin` to PATH to ensure command detection | |
export PATH=$PATH:/sbin:/usr/sbin | |
# Function to check if a command exists | |
check_command() { | |
if ! command -v "$1" &> /dev/null; then | |
echo "Error: '$1' is not installed or not accessible." | |
missing_commands+=("$1") | |
fi | |
} | |
# Function to gather disk information | |
gather_disk_info() { | |
echo "===================================================" | |
echo | |
echo "-- PARTED DISK INFO --" | |
echo "----------------------" | |
sudo parted -ls | |
echo | |
echo "-- INXI DISK INFO --" | |
echo "--------------------" | |
sudo inxi -DpRjlLoux | |
echo | |
echo "-- LSBLK DISK INFO --" | |
echo "---------------------" | |
sudo lsblk -fm | |
echo | |
echo "===================================================" | |
} | |
# Check for required commands | |
missing_commands=() | |
check_command "parted" | |
check_command "inxi" | |
check_command "lsblk" | |
# If any command is missing, prompt the user to install them | |
if [ ${#missing_commands[@]} -ne 0 ]; then | |
echo "The following commands are required but not installed: ${missing_commands[*]}" | |
echo "Please install them using your package manager. For example:" | |
echo " sudo apt install parted inxi util-linux # For Debian/Ubuntu" | |
echo " sudo dnf install parted inxi util-linux # For Fedora/RHEL" | |
echo " sudo pacman -S parted inxi util-linux # For Arch Linux" | |
exit 1 | |
fi | |
# Prompt the user for their choice | |
echo "Do you want to save the disk information to a file or display it on the terminal?" | |
echo "Enter '1' to save to a file or '2' to display on the terminal." | |
read -p "Your choice (1/2): " choice | |
if [[ "$choice" == "1" ]]; then | |
# Save the output to a file | |
output_file=~/diskinfo.txt | |
{ | |
echo | |
echo "-- PARTED DISK INFO --" | |
echo "----------------------" | |
sudo parted -ls | |
echo | |
echo "-- INXI DISK INFO --" | |
echo "--------------------" | |
sudo inxi -DpRjlLoux | |
echo | |
echo "-- LSBLK DISK INFO --" | |
echo "---------------------" | |
sudo lsblk -fm | |
echo | |
echo "===================================================" | |
} > "$output_file" 2>&1 | |
echo "Disk information has been saved to $output_file" | |
elif [[ "$choice" == "2" ]]; then | |
# Display the output in the terminal | |
gather_disk_info | |
else | |
echo "Invalid choice. Please run the script again and choose either '1' or '2'." | |
exit 1 | |
fi |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment