Home Linux Commands How To Gather Comprehensive Disk Information On Linux

How To Gather Comprehensive Disk Information On Linux

By sk
Published: Updated: 367 views

Understanding the disk configuration and health of your Linux system is important for system administration, troubleshooting, and optimization. In this blog post, we'll walk you through a detailed process of displaying or gathering comprehensive disk information in Linux using popular utilities like parted, inxi, and lsblk.

By the end of this guide, you'll have a clear understanding of how to collect and analyze disk data, which can be useful for maintaining and optimizing your Linux environment.

Let us get started.

Step-by-Step Guide to Gathering Disk Information in Linux

1. Preparing the Output File

The first step is to create a text file where all the disk information will be stored. This file will be located in your home directory and named drives.txt.

echo —PARTED DISK INFO— > /home/$USER/diskinfo.txt 2>&1

This command initializes the diskinfo.txt file with a header indicating that the following information will be related to disk partitions.

2. Using parted to List Disk Partitions

Next, we use the parted utility to list all disk partitions on the system. This command requires superuser privileges, so we use sudo.

sudo parted -ls >> /home/$USER/diskinfo.txt 2>&1

The output of this command is appended to the drives.txt file, providing detailed information about each disk partition.

3. Gathering Disk Information with inxi

The inxi utility is a powerful tool for gathering system information, including detailed disk data. We append the output of inxi to our diskinfo.txt file.

echo —INXI DISK INFO— >> /home/$USER/diskinfo.txt 2>&1
sudo inxi -DpRjlLoux >> /home/$USER/diskinfo.txt 2>&1

This command not only provides disk information but also includes details about processes, system resources, and more.

4. Listing Block Devices with lsblk

Finally, we use the lsblk utility to list block devices, including detailed filesystem and permission information.

echo —LSBLK DISK INFO— >> /home/$USER/diskinfo.txt.txt
sudo lsblk -fm >> /home/$USER/diskinfo.txt 2>&1

This command appends the block device information to our diskinfo.txt file, completing the collection process.

Analyzing the Collected Data

Once the diskinfo.txt file is generated, you can open it using any text editor to analyze the disk information.

The file will contain sections labeled with headers like "—PARTED DISK INFO—", "—INXI DISK INFO—", and "—LSBLK DISK INFO—".

Each section provides different perspectives on the disk configuration, allowing you to cross-reference and verify the data.

Here's the output of diskinfo.txt file in my Debian 12 system:

—PARTED DISK INFO—
Model: JMicron Generic (scsi)
Disk /dev/sda: 1000GB
Sector size (logical/physical): 512B/4096B
Partition Table: msdos
Disk Flags:

Number Start End Size Type File system Flags
1 1049kB 1000GB 1000GB primary boot
2 1000GB 1000GB 33.6MB primary fat16 esp


Model: WD_BLACK SN750 SE 500GB (nvme)
Disk /dev/nvme0n1: 500GB
Sector size (logical/physical): 512B/512B
Partition Table: gpt
Disk Flags:

Number Start End Size File system Name Flags
1 1049kB 538MB 537MB fat32 boot, esp
2 538MB 499GB 499GB ext4
3 499GB 500GB 1023MB linux-swap(v1) swap


—INXI DISK INFO—
Logical:
Message: No logical block device data found.
RAID:
Message: No RAID data found.
Drives:
Local Storage: total: 1.36 TiB used: 1.05 TiB (77.0%)
ID-1: /dev/nvme0n1 vendor: Western Digital model: WD BLACK SN750 SE 500GB
size: 465.76 GiB temp: 38.9 C
ID-2: /dev/sda type: USB vendor: Western Digital model: WDS100T2G0A-00JH30
size: 931.51 GiB
Partition:
ID-1: / size: 455.95 GiB used: 269.25 GiB (59.1%) fs: ext4
dev: /dev/nvme0n1p2 label: N/A uuid: 99406049-9ff5-47d1-a1ce-d5e27cd859c0
ID-2: /boot/efi size: 511 MiB used: 9.2 MiB (1.8%) fs: vfat
dev: /dev/nvme0n1p1 label: N/A uuid: CF87-3143
ID-3: /etc/pve size: 128 MiB used: 16 KiB (0.0%) fs: fuse dev: /dev/fuse
label: N/A uuid: N/A
ID-4: /media/ostechnix/SK_WD_SSD size: 931.45 GiB used: 806.62 GiB (86.6%)
fs: exfat dev: /dev/sda1 label: SK_WD_SSD uuid: 2A81-C276
Swap:
ID-1: swap-1 type: partition size: 976 MiB used: 256 KiB (0.0%)
dev: /dev/nvme0n1p3 label: N/A uuid: 1ff239fc-674c-4749-9c73-fe5d0668071a
Unmounted:
ID-1: /dev/sda2 size: 32 MiB fs: vfat label: VTOYEFI uuid: 5A89-BA75
—LSBLK DISK INFO—
NAME FSTYPE FSVER LABEL UUID FSAVAIL FSUSE% MOUNTPOINTS SIZE OWNER GROUP MODE
sda 931.5G root disk brw-rw----
├─sda1 exfat 1.0 SK_WD_SSD 2A81-C276 124.8G 87% /media/ostechnix/SK_WD_SSD 931.5G root disk brw-rw----
└─sda2 vfat FAT16 VTOYEFI 5A89-BA75 32M root disk brw-rw----
nvme0n1 465.8G root disk brw-rw----
├─nvme0n1p1 vfat FAT32 CF87-3143 501.8M 2% /boot/efi 512M root disk brw-rw----
├─nvme0n1p2 ext4 1.0 99406049-9ff5-47d1-a1ce-d5e27cd859c0 163.5G 59% / 464.3G root disk brw-rw----
└─nvme0n1p3 swap 1 1ff239fc-674c-4749-9c73-fe5d0668071a [SWAP] 976M root disk brw-rw----

Automate Collecting Disk Information using a Script

Below is a Bash script that automates the process of gathering disk information using the parted, inxi, and lsblk utilities.

The script prompts the user to choose whether they want to save the output to a file or display it in the standard output.

It also checks if the required commands are installed and provides instructions on how to install them if they are missing.

Script: diskinfo.sh

#!/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

How to Use the Script to Display or Save the Disk Information in a File

Copy the script above into a file named diskinfo.sh.

Run the following command to make the script executable:

chmod +x diskinfo.sh

Execute the script by running:

./diskinfo.sh

This script will save the extensive disk information in a text file named diskinfo.txt in your HOME directory.

You can also move the script to your system's PATH and execute it from anywhere. Common choices include /usr/local/bin or /usr/bin. For example:

sudo mv diskinfo.sh /usr/local/bin/diskinfo

Restart your current shell session or log out and log back in. From now on, you can simply run the diskinfo command from anywhere to display comprehensive information about your disk.

Script Explanation:

  • check_command Function: This function checks if a command is installed. If not, it provides installation instructions and exits the script.
  • User Prompt: The script prompts the user to choose between saving the output to a file or displaying it in the standard output.
  • Output Handling: Depending on the user's choice, the script either saves the output to a file (diskinfo.txt in the user's home directory) or displays it directly in the terminal.
  • Error Handling: The script checks for the presence of required commands (parted, inxi, lsblk) and provides installation instructions if any are missing.

This script automates the process of gathering disk information and provides a user-friendly interface for choosing the output method.

It also ensures that all required tools are installed, making it easier for users to troubleshoot and maintain their Linux systems.

Conclusion

Gathering comprehensive disk information on a Linux system is a fundamental task for system administrators and developers.

In this guide, we learned a step-by-step approach to gathering disk information in Linux by using the parted, inxi, and lsblk utilities. These utilities will help you to efficiently collect and analyze disk data, ensuring your system is running optimally.

We also created a simple Bash script to automate this process. This script includes more robust error handling and checks, ensuring it correctly identifies installed commands and provides helpful installation instructions if any are missing.

Related Read:

You May Also Like

Leave a Comment

* By using this form you agree with the storage and handling of your data by this website.

This site uses Akismet to reduce spam. Learn how your comment data is processed.

This website uses cookies to improve your experience. By using this site, we will assume that you're OK with it. Accept Read More