1

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 )

linfan
  • 11
  • 1
  • 2

3 Answers3

3

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
Kent
  • 189,393
  • 32
  • 233
  • 301
  • Requires a relatively recent version of `df` but very much the right answer. Now if only you could tell `df` to not spit out the headers. =) – Etan Reisner Jan 23 '15 at 13:20
  • @EtanReisner Exactly. File a request on their project page. I recently submitted a bug because if the stat command and i got fixed right away. Might be worth a try. – WGRM Jul 02 '21 at 20:27
2

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
nlu
  • 1,903
  • 14
  • 18
  • 1
    I'm pretty sure by definition the `device` of the folder is the same as that of its mountpoint (unless you were using the `-L` option). So just use `stat -c '%D` or similar? – sehe Jan 23 '15 at 12:48
  • 1
    The device can be found with just `df directory`. – Walter Jan 23 '15 at 12:52
  • @sehe In my version of stat, those are exactly the format option, that return nothing but an error. I recently placed a bug, too. Stat's major minor output in dec was complete rubbish. (made no sense) (Core Utils 8.3) But using stat is the obvious way, since every other program used will exactly do the same; calling stat. – WGRM Jul 02 '21 at 20:31
0

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
pchero
  • 11
  • 4