Ask Ubuntu is a question and answer site for Ubuntu users and developers. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

there's a way to get the device name (/dev/sdx) with the mounted folder? i mean, get the assosiated device in a mount point using the mounted folder as the reference, or make a python dictionary with the mount points : devices...

i know that mount -l can get me the mounted file system info, but i don't really know how to strip it to make the dictionary...

any help?


share|improve this question
See this python udisks - enumerating device information at Stackoverflow and this could help too: How to find the mountpoint a file resides on?. – gertvdijk Sep 18 '12 at 0:51
up vote 6 down vote accepted

This is totally unrelated to Ubuntu, but here you are:

#!/usr/bin/env python

import subprocess

mounts = {}

for line in subprocess.check_output(['mount', '-l']).split('\n'):
    parts = line.split(' ')
    if len(parts) > 2:
        mounts[parts[2]] = parts[0]

print mounts
share|improve this answer
thank, this did the job, but it's having problems when a tag has a space on it, it only displays the first word... – Hairo Sep 18 '12 at 1:34
The code is just an illustration, I didn't try to write a robust production ready program. You can try using cat /proc/mounts command instead of mount - maybe it's output is easier to parse. – Sergey Sep 18 '12 at 1:54
thanks again, i managed to get it working using cat /proc/mounts and replacing some things with string.replace() – Hairo Sep 18 '12 at 2:54
Why not create a Java app for doing what can be achieved with a 27 character command line? Sorry, couldn't resist. – January Sep 18 '12 at 8:03
@January: Firstly, it's Python :) Secondly, the OP stated she/he needs a Python dictionary, supposedly to be used in an application. Piping the output through cut does not solve the problem of converting the output into something meaningful for a program. – Sergey Sep 18 '12 at 8:14

Why not use the Gio interface? I like these solutions better than executing a bash command and parsing its output. This way you can catch exceptions and have more control.

Here's a little example:

>>> from gi.repository import Gio
>>> vm = Gio.VolumeMonitor.get()
>>> for v in vm.get_volumes():
...     print v.get_name()

See the documentation for much more interesting methods.




share|improve this answer
mm... this looks better, i'll try it when i get home :) – Hairo Sep 18 '12 at 13:36


mount | cut -f 1,3 -d ' '

Explanation: cut is a handle little tool for splitting lines using a delimiter character (specified by the -d option) and selecting some of the fields for output (using a comma separated argument list to the -f option). Since the mount output is regular and delimited by spaces, the above outputs the first and third column, omitting "on" and the remainder of the line.

share|improve this answer

Your Answer


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.