how to get uuid of filesystem given a path?

4.5k Views Asked by At

I am handed a path of a directory ( sometimes path of a file ).

Which utility / shell script will reliably give me the UUID of the filesystem on which is this directory ( or file ) located / stored ?

( by UUID of filesystems I mean the "UUID=..." entry as shown by e.g. blkid )

( this is happnening on a redhat linux )

3

There are 3 best solutions below

0
On

Simply, you can type like this,

pchero@mywork:~$ ls -l /dev/disk/by-uuid/
total 0
lrwxrwxrwx 1 root root 10 Jan 23 09:03 0267689b-b929-4f30-b8a4-08c742f0746f -> ../../sda2
lrwxrwxrwx 1 root root 10 Jan 23 09:03 2d682ea1-dab0-49ba-a77a-9335ccd47e58 -> ../../sda3
lrwxrwxrwx 1 root root 10 Jan 23 09:03 64e733e9-2e6a-4d3e-aabe-d0d26fbfc991 -> ../../sda1
lrwxrwxrwx 1 root root 10 Jan 23 09:03 a99fb356-4e01-4a1c-af41-001b0fd8a844 -> ../../sdb1
lrwxrwxrwx 1 root root 10 Jan 23 09:03 f2f7618e-76c5-4e9a-9657-e002d9a66ccf -> ../../sda4
3
On

Find the device of the mount point of the path,

DEVICE=$(df /path/to/some_file_or_directory | grep "$MOUNTPOINT\$"| cut -f1 -d" ")

and get the UUID of the device:

sudo blkid $DEVICE
2
On

give this line a try:

 sudo blkid -o value $(\df --output=source "$file"|tail -1)|head -1

in above line, $file is the variable to save the file/dir. You may want to check if the file/dir exists, before call the line.

And this line needs root permission (sudo)

\df is just for avoiding to use alias if you had one, for example with -T option, it conflicts with --output

Some test :

kent$  file="/home/kent/.vimrc"
kent$  sudo blkid -o value $(\df --output=source "$file"|tail -1)|head -1
9da1040a-4a24-4a00-9c62-bad8cc3c028d 

kent$  file="/etc"
kent$  sudo blkid -o value $(\df --output=source "$file"|tail -1)|head -1
2860a386-af71-4a28-86d7-00ccf5d12b4d