This question already has an answer here:

Using Python, I scan Unix mounted volumes for files, then add or purge the filenames in a database, based on their existence. I've just realised that if the volume being scanned is unmounted for some reason, the scan will assume every filename on that volume should be purged! Yikes. Is there any better way to mount volumes or any suggestions at all?

About the only thing I can think of is to put a permanent dummy file on each volume which I check for before scanning, thereby ensuring that the volume is only scanned if the dummy file can be located.

marked as duplicate by Sergiy Kolodyazhnyy, Stephen Rauch, Jeff Schaller, Archemar, Anthon Jul 26 '17 at 5:16

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.


You can use mountpoint to check if the given directory is a mount point. Example

mountpoint /mnt/foo; printf "$?\n"
/dev/foo is a mountpoint

mountpoint /mnt/bar; printf "$?\n"
/dev/bar is not a mountpoint

As the return value indicates, this can easily by used in an if statement in a script.

  • 1
    Wow, and Python even has the ismount method. Thank you – MFB Jun 10 '15 at 10:39
  • I'd recommend using a different approach. mountpoint(1) from util-linux-2.26 doesn't seem to do what you expect. From util-linux-2.26/sys-utils/mountpoint.1: mountpoint checks whether the given directory or file is mentioned in the /proc/self/mountinfo file. This means it only "sees" mount points that are already mounted, which pretty much defeats the initial purpose. Also, FWIW, mountpoint(1) is Linux-specific. – lcd047 Jun 10 '15 at 16:10
  • I agree it's Linux specific. In general I don't think there's a better alternative to parsing the mount or df output. I understand the question that the OP wants to check if that directory is a mount point at this moment and that's what mountpoint does. – Marco Jun 10 '15 at 16:46

A directory is a mount point if the st_dev field returned by stat is different from the value in its parent. This is the criterion that most tools use (find -xdev, rsync -x, du -x, …). This is provided as the os.path.ismount function in Python.

This won't help you directly, because if a directory is empty, that doesn't mean that it was a mount point at some other time. If you want to detect mount points, you need to do it at indexing time. You may want to keep separate mount points in separate indexes, and only update indexes whose roots are present and non-empty. Beware that a directory that is sometimes a mount point may be non-empty even when the usual filesystem isn't mounted, either because some other filesystem is mounted there or simply because there are files in that directory (the files in a directory are hidden while this directory is a mount point).

  • Also beware of btrfs and potential other file systems with subvolume features. In those cases the device field will be different and yet it's neither a mount point (not in mtab or similar) nor is it a separate partition. – 0xC0000022L Jun 10 '15 at 23:56

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