7

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 ?

  • 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 May 20 '11 at 12:00

4 Answers4

10

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
      ...
msrd0
  • 240
  • 3
  • 13
2

mount knows this.

Example:

mount | grep /dev/sdc1

Or (likely to be faster):

grep '/dev/sdc1' /etc/mtab
Bart De Vos
  • 17,761
  • 6
  • 62
  • 81
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.

jmtd
  • 575
  • 2
  • 6
2

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.

mivk
  • 3,457
  • 1
  • 34
  • 29