Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

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 ?

share|improve this question
    
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
add comment

3 Answers

mount knows this.

Example:

mount | grep /dev/sdc1

Or (likely to be faster):

grep '/dev/sdc1' /etc/mtab
share|improve this answer
1  
Ah, I hadn't understood the question then. You can also use /etc/mtab. –  Eduardo Ivanec May 20 '11 at 12:18
    
Didn't know that, added it :-) –  Bart De Vos May 20 '11 at 12:22
    
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) :) –  teddypickerfromul May 20 '11 at 12:33
add comment

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.

share|improve this answer
add comment

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.

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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