How to test if a given path is a mount point


Question

Suppose do you want test if /mnt/disk is a mount point in a shell script. How do you do this?

1
34
1/26/2009 9:33:51 AM

Accepted Answer

I discover that on my Fedora 7 there is a mountpoint command.

From man mountpoint:

NAME
       mountpoint - see if a directory is a mountpoint

SYNOPSIS
       /bin/mountpoint [-q] [-d] /path/to/directory
       /bin/mountpoint -x /dev/device

Apparently it come with the sysvinit package, I don't know if this command is available on other systems.

[root@myhost~]# rpm -qf $(which mountpoint)
sysvinit-2.86-17
52
1/27/2009 1:55:40 PM

Not relying on mount, /etc/mtab, /proc/mounts, etc.:

if [ `stat -c%d "$dir"` != `stat -c%d "$dir/.."` ]; then
    echo "$dir is mounted"
else
    echo "$dir is not mounted"
fi

When $dir is a mount point, it has a different device number than its parent directory.

The benefit over the alternatives listed so far is that you don't have to parse anything, and it does the right thing if dir=/some//path/../with///extra/components.

The downside is that it doesn't mark / as a mountpoint. Well, that's easy enough to special-case, but still.


Licensed under: CC-BY-SA with attribution
Not affiliated with: Stack Overflow
Icon