How to get the mount point of flash drive by using uuid?

5,312

Solution 1

What you're after is findmnt. For example:

$ findmnt -rn -S UUID=xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx -o TARGET
/mnt/mountpoint

or

$ findmnt -rn -S PARTUUID=xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx -o TARGET
/mnt/mountpoint

If nothing is mounted matching that UUID, nothing is output and the return code is 1 (failure), otherwise, the mountpoint is output and the return code is 0 (success).

Explanation of options

-r, --raw              use raw output format
-n, --noheadings       don't print column headings
-S, --source <string>  the device to mount (by name, maj:min, 
                         LABEL=, UUID=, PARTUUID=, PARTLABEL=)
-o, --output <list>    the output columns to be shown

Available columns:
      ...
      TARGET  mountpoint
      ...

Solution 2

The kernel's mount table is at /proc/mounts. This is slightly more reliable than /etc/mtab, because a system/software error may result in the mtab being corrupted or not written to when it should be.

Solution 3

To find the actual device from the UUID, blkid may be better than your readlink solution, which relies on udev.

myuuid="7DCD-9380"
mydev=$(blkid -l -o device -t UUID="$myuuid")

To get the mount point for this device, you can use this:

grep $mydev /proc/mounts | cut -d' ' -f 2

or

df -P | grep $mydev | awk '{print $6}'

The latter is more portable, because /proc is Linux-only. The df solution with the -P (POSIX) option should also work on Mac and other Unix systems.

Of course, both would break if your mount point contained spaces. But nobody mounts stuff in "/mnt/evil mount point/", right?

If you fear your predecessor might have done just that, you can use perl instead of awk:

df -P | grep $mydev | perl -pe 's/^(\S+\s+){5}//'

(the grep could also be handled by perl, but it may be harder to read for some)

The perl regex replaces 5 groups of non-spaces+spaces with nothing, leaving only the rest of line. Which is the mount point including any possible spaces.

Solution 4

mount knows this.

Example:

mount | grep /dev/sdc1

Or (likely to be faster):

grep '/dev/sdc1' /etc/mtab
Share:
5,312

Related videos on Youtube

teddypickerfromul
Author by

teddypickerfromul

Updated on September 18, 2022

Comments

  • teddypickerfromul
    teddypickerfromul over 1 year

    I am beginner in ubuntu linux and i need to write simple bash script, that can identify necessary flash drive(which contains only one vfat partition) using uuid of this partition, and get the mount point of this flash drive.The /etc/fstab file does'nt contains mountig rule for this drive. For example, let partition uuid as 7DCD-9380 Using the readlink tool i can get device link in /dev catalog :

    teddy@st1:~$ readlink -f /dev/disk/by-uuid/7DCD-9380  
    /dev/sdc1
    

    But how i can get mount point of /dev/sdc1 device ?

    • Admin
      Admin almost 13 years
      You could mount it wherever you want, for example on /mnt/flashdrive (after mkdir /mnt/flashdrive of course). What specific mount point are you looking for?
  • Eduardo Ivanec
    Eduardo Ivanec almost 13 years
    Ah, I hadn't understood the question then. You can also use /etc/mtab.
  • APR
    APR almost 13 years
    Didn't know that, added it :-)
  • teddypickerfromul
    teddypickerfromul almost 13 years
    Thanks for your advice, i hoped to find easier way to get path to catalog where my flash is mounted(without using awk/sed), but but it seems there is no such easy way(such as getting link of partition in /dev catalog using it uuid) :)
  • Dennis Nolte
    Dennis Nolte over 9 years
    System error or / beeing mounted read only.