Is there an easy way to determine the hexadecimal ID of the filesystem for a given partition? Say, I have a /dev/sda1 partition which is of "Linux" type having the filesystem ID of 83 (as shown by fdisk for example). I need to get this number from a bash script without parsing the output of fdisk/sfdisk.
Asked
Active
Viewed 1,352 times
2
-
What's wrong with parsing fdisk/sfdisk output ? – jlliagre May 31 '12 at 15:09
-
The wrong part is that I know how to do it. :-) I thought that perhaps there was an easier way to do the same, like __some_command /dev/sda1; output: 83__. I.e. no need to do parsing. In addition to that, some devices do not have any partition. So using fdisk/sfdisk will bring nothing. – azerIO May 31 '12 at 15:12
-
1Your other alternative is to go digging around in the partition table. – Dennis Williamson May 31 '12 at 15:18
1 Answers
3
The partition IDs are stored in the MBR (or EMBR for logical ones). They are thus not stored in the device itself. i.e. /dev/sda1 doesn't contains its type.
fdisk and similar commands do display the partition types and are designed to do it properly. Trying to avoid them would be pointless.
This Linux shell script will show the partition ID for a given device:
#!/bin/sh
fdisk -l | tr -d '*' | awk -v dev=$1 '$1 == dev { print $5 } '
.
# some_command /dev/sda1
83

jlliagre
- 29,783
- 6
- 61
- 72
-
1Thx for the answer, but your command won't work. Don't forget that depending on the partition type a line might contain the * (star) symbol meaning that its a bootable partition, which screws up the work of awk. – azerIO Jun 01 '12 at 09:37
-
1