Created
October 6, 2025 14:20
-
-
Save Natrinicle/3539a80d53bf9bf58f467a7b4c028db9 to your computer and use it in GitHub Desktop.
Find Drive Serial Numbers that are Failing According to SMART using BASH
This file contains hidden or 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
| function failing_drive_serial_numbers () { | |
| shopt -s nullglob | |
| local drives=() | |
| local nvme_devices=($(lsblk -dno name,tran 2>/dev/null | awk '$2 == "nvme" {print "/dev/" $1}')) | |
| local ssd_devices=($(lsblk -dno name,rota,tran 2>/dev/null | awk '$2 == "0" && $3 != "nvme" {print "/dev/" $1}')) | |
| local hdd_devices=($(lsblk -dno name,rota 2>/dev/null | awk '$2 == "1" {print "/dev/" $1}')) | |
| if [ "$#" -eq 0 ]; then | |
| set -- "$@" "all" | |
| fi | |
| for arg in "$@"; do | |
| case "$arg" in | |
| all|nvme) | |
| drives+=("${nvme_devices[@]}") | |
| ;; | |
| all|sdd) | |
| drives+=("${ssd_devices[@]}") | |
| ;; | |
| all|hdd) | |
| drives+=("${hdd_devices[@]}") | |
| ;; | |
| *) | |
| # For non-keywords, add directly (user's shell likely already expanded wildcards like /dev/sd?) | |
| drives+=("$arg") | |
| ;; | |
| esac | |
| done | |
| # Deduplicate drives | |
| drives=($(printf '%s\n' "${drives[@]}" | sort -u | tr '\n' ' ')) | |
| for drive in "${drives[@]}"; do | |
| if sudo smartctl -a "${drive}" | grep -q "Drive failure expected in less than 24"; then | |
| sudo smartctl -a "${drive}" | grep "Serial Number" | |
| fi | |
| done | |
| } | |
| function failing_nvme_serial_numbers () { | |
| failing_drive_serial_numbers nvme | |
| } | |
| function failing_sdd_serial_numbers () { | |
| failing_drive_serial_numbers sdd | |
| } | |
| function failing_hdd_serial_numbers () { | |
| failing_drive_serial_numbers hdd | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment