Get UUID of / filesystem from script
Use findmnt
:
$ findmnt /
TARGET SOURCE FSTYPE OPTIONS
/ /dev/md127p1 ext4 rw,relatime,stripe=256,data=ordered
$ findmnt / -o UUID
UUID
046a554b-d9f5-4b23-82e6-ffaeb98284aa
$ findmnt / -o UUID -n
046a554b-d9f5-4b23-82e6-ffaeb98284aa
It also has several options to control how it looks up information, and how it presents it (including JSON output!). It's part of the mount
package, so available on any Ubuntu installation.
Another solution:
lsblk -nr -o UUID,MOUNTPOINT | grep -Po '.*(?= /$)'
-n
suppresses the header (not really needed, but safer for parsing)-r
makes raw output (makes it safer to parse)-o UUID,MOUNTPOINT
include only necessary information
You can use the lsblk
command to output the UUID, but you need the device name of the partition (such as /dev/sda2). You can get this by using the df
command and trimming the output. Use command substitution to give the device name to lsblk
. It appears you need sudo to access the UUID, although the normal output of lsblk does not require it:
sudo lsblk -n -o UUID "$(df -P / | tail -n1 | cut -d' ' -f1)"