Skip to content

Instantly share code, notes, and snippets.

@discordianfish
Created January 9, 2022 13:02
Show Gist options
  • Save discordianfish/e9e7e01daf1130a1078925744b34a037 to your computer and use it in GitHub Desktop.
Save discordianfish/e9e7e01daf1130a1078925744b34a037 to your computer and use it in GitHub Desktop.
List disks with path and id(s)
#!/bin/bash
set -euo pipefail
declare -A paths
declare -A ids
for f in /dev/disk/by-path/*; do
[[ "$f" == *"-part"* ]] && continue
paths[$(readlink -f "$f")]+="$(basename "$f") "
done
for f in /dev/disk/by-id/*; do
[[ "$f" == *"-part"* ]] && continue
ids[$(readlink -f "$f")]+="$(basename "$f") "
done
for dev in "${!paths[@]}"; do
printf '%s\t%s\t%s\n' "$dev" "${paths[$dev]}" "${ids[$dev]}"
done
@discordianfish
Copy link
Author

Sample output (redacted IDs):

$ ./lsdisks
/dev/sdh        pci-0000:00:14.0-ata-7  ata-ST5000LM000-2AN170_xxx wwn-yyy
/dev/sdg        pci-0000:00:14.0-ata-6  ata-ST5000LM000-2AN170_xxx wwn-yyy
/dev/sdf        pci-0000:00:14.0-ata-5  ata-Samsung_SSD_850_EVO_500GB_xxxx wwn-yyyy
/dev/sde        pci-0000:00:14.0-ata-4  ata-ST5000LM000-2AN170_xxx wwn-yyy
/dev/sdd        pci-0000:00:13.0-ata-4  ata-ST5000LM000-2AN170_xxx wwn-yyy
/dev/sdc        pci-0000:00:13.0-ata-3  ata-ST5000LM000-2AN170_xxx wwn-yyy
/dev/sdb        pci-0000:00:13.0-ata-2  ata-ST5000LM000-2AN170_xxx wwn-yyy
/dev/sda        pci-0000:00:13.0-ata-1  ata-ST5000LM000-2AN170_xxx wwn-yyy

Sorted by path by default. It's tab delimited, so you can use sort for other sorting, eg by ID:

$ ./lsdisks |sort -k3
/dev/sdh        pci-0000:00:14.0-ata-7  ata-ST5000LM000-2AN170_xxx wwn-yyy
/dev/sdc        pci-0000:00:13.0-ata-3  ata-ST5000LM000-2AN170_xxx wwn-yyy
/dev/sda        pci-0000:00:13.0-ata-1  ata-ST5000LM000-2AN170_xxx wwn-yyy
/dev/sdb        pci-0000:00:13.0-ata-2  ata-ST5000LM000-2AN170_xxx wwn-yyy
/dev/sdd        pci-0000:00:13.0-ata-4  ata-ST5000LM000-2AN170_xxx wwn-yyy
/dev/sde        pci-0000:00:14.0-ata-4  ata-ST5000LM000-2AN170_xxx wwn-yyy
/dev/sdg        pci-0000:00:14.0-ata-6  ata-ST5000LM000-2AN170_xxx wwn-yyy
/dev/sdf        pci-0000:00:14.0-ata-5  ata-Samsung_SSD_850_EVO_500GB_S2RBNX0JC50161K wwn-0x5002538d4284b31f

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment