How do I get the UUID of a partition and define a Bash variable as being equal to it?
Solution 1
Note that's the UUID of the filesystem (or other structured data with a UUID the udev scripts know about) on the partition, not the UUID of the partition itself (not all partitioning schemes give UUIDs to partition anyway). See also Difference between UUID from blkid and mdadm?.
A few options on Linux-based systems to get the FS UUID:
fs_uuid=$(blkid -o value -s UUID /dev/sdb1)
fs_uuid=$(lsblk -no UUID /dev/sdb1)
fs_uuid=$(udevadm info -n sdb1 -q property | sed -n 's/^ID_FS_UUID=//p')
fs_uuid=$(find /dev/disk/by-uuid -lname '*/sdb1' -printf %f)
The first one may require superuser privileges or at least the right to read the device.
If the filesystem is mounted, you can also use:
fs_uuid=$(findmnt -fn -o UUID /dev/sdb1)
Solution 2
You can do it in this way
PART_ID=$(blkid -o value -s UUID /dev/sdb1)
Related videos on Youtube
identify
Updated on September 18, 2022Comments
-
identify over 1 year
I would like to define a Bash variable
PART_ID
as being equal to the UUID of the/dev/sdb1
partition. The closest I have gotten to the desired answer is the output of:ls -ld /dev/disk/by-uuid/* | grep sdb1
which, for me, gives:
lrwxrwxrwx 1 root root 10 Mar 16 17:02 /dev/disk/by-uuid/d26c3e60-0cfb-4118-9dec-1f1819439790 -> ../../sdb1
which is not an acceptable value for me to set
PART_ID
to. Rather whatPART_ID
should equal isd26c3e60-0cfb-4118-9dec-1f1819439790
.-
don_crissti about 8 yearsThis has been asked before, seach...
my_uuid=$(lsblk /dev/sdb1 -no UUID)
-
-
David Baird almost 5 years
fs_uuid=$(lsblk -ndo UUID /dev/sdb1)
(with the additional-d
flag) may be more effective if /dev/sdb1 has descendants, as can be the case if used in a RAID, LVM, or as an encrypted partition.