Suppose I have a disk located at /dev/DISK_NAME. I'm curious if there is a command to find the nth partition of this disk.
It seems like this sort of thing should be possible with lsblk. The best I can come up with is:
lsblk -nlpo NAME,TYPE /dev/DISK_NAME | awk '/part$/ {print $1}' | grep "[^0-9]n$"
Here, the n in the last grep represents the partition number in question.
The command above works for the arch linux machine I'm currently on, where DISK_NAME=sda and the output of lsblk -nlpo NAME,TYPE is
/dev/sda disk
/dev/sda1 part
/dev/sda2 part
/dev/sda3 part
/dev/sda5 part
My command also works on my arch linux machine where DISK_NAME=nvme0n1 and the output of lsblk -nlpo NAME,TYPE is
/dev/nvme0n1 disk
/dev/nvme0n1p1 part
/dev/nvme0n1p2 part
Still, this feels rather hacky, and I'm not sure if it would work on an arbitrary disk.
Are there better ways to do this?