11

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)

2
  • 3
    See unix.stackexchange.com/questions/11311/… on how to get the filesystem, following which you can run blkid. Commented Jan 23, 2015 at 12:55
  • Note that not all file systems will have a UUID; blkid won't give you a UUID eg. for NFS mounts. Commented Jan 23, 2015 at 18:09

2 Answers 2

11

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
  • 4
    There's no need for stat: findmnt -no uuid -T "$path" Commented Sep 24, 2015 at 14:07
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" ) 
2
  • Better would be -o export, since that prints output of the form UUID=.... Commented Jan 23, 2015 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. Commented Jan 24, 2015 at 17:32

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.