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)

  • 3
    See unix.stackexchange.com/questions/11311/… 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

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.

  • 1
    There's no need for stat: findmnt -no uuid -T "$path" – don_crissti Sep 24 '15 at 14:07

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:

blkid -s UUID -o value $( df "$FILE_OR_DIR" )
  • 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. – roaima Jan 24 '15 at 17:32

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.