10

I am handed a path of a directory or a file.

Which utility/shell script will reliably give me the UUID of the file system on which is this directory/file located?

By UUID of file system I mean the UUID=... entry as shown by e.g. blkid

I'm using Redhat Linux.

(someone suggested that I should ask this here at unix.stackexchange.com, so I moved it from the original stackexchange.com)

jimmij
  • 47,140
linfan
  • 103
  • 1
  • 4
  • 3
    See http://unix.stackexchange.com/questions/11311/how-do-i-find-on-which-physical-device-a-folder-is-located on how to get the filesystem, following which you can run blkid. – muru Jan 23 '15 at 12:55
  • Note that not all file systems will have a UUID; blkid won't give you a UUID eg. for NFS mounts. – oliver Jan 23 '15 at 18:09

2 Answers2

9

One option is stat + findmnt combo:

findmnt -n -o UUID $(stat -c '%m' "$path")

Here -n disables header, and -o UUID prints only UUID value. Option -c '%m' of stat is present to output only mountpoint of given path.

jimmij
  • 47,140
3

You can use df to find the file's mount point, and then apply the result to blkid to the the UUID. You need to run (at least) blkid as root for this to work:

FILE_OR_DIR="$PWD"
blkid -s UUID -o value $( df "$FILE_OR_DIR" )
Chris Davies
  • 116,213
  • 16
  • 160
  • 287
  • Better would be -o export, since that prints output of the form UUID=.... – muru Jan 23 '15 at 17:04
  • @muru it wasn't clear to me whether just the UUID was required (-o value) or a UUID="value" type expression (-o export). I chose value. – Chris Davies Jan 24 '15 at 17:32