Given a block device (e.g,
/dev/sda), how to determine the names of its partitions (if any) in a script (i.e. without user interaction)?
lsblk prints out all related block devices. This includes the partitions of said device. Since it of course includes the device itself and
lsblk does not allow for excluding specific devices the example solution below simply uses inverted
lsblk -o KNAME -n /dev/mmcblk0 | grep -v "^mmcblk0$"
For full paths (which also simplifies the generation of the grep string) one can use
lsblk -po KNAME -n /dev/mmcblk0 | grep -v "^/dev/mmcblk0$"
There are a couple of tools which you can use for this. One of the tools which I tend to use a lot is
fdisk. fdisk is something which will list the partitions of your block device and also state the exact sizes for you.
fdisk is quite a versatile program which can also modify the internal properties of your partitions in terms of storage so be very careful using it.
In the case of listing a partition you simply want to execute the following command:
fdisk -l <name of block device>. From this point you're then able to see the exact sizes, name and type of the partitions you seek. If you would like to use
fdisk further for other uses relating to the hard disk then use the
man command to open the manual page for