[ACCEPTED]-How to test if a given path is a mount point-shell

Accepted answer
Score: 56

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

From man mountpoint:

       mountpoint - see if a directory is a mountpoint

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

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

[root@myhost~]# rpm -qf $(which mountpoint)
Score: 23

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

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

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

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

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

Score: 5

Using GNU find

find <directory> -maxdepth 0 -printf "%D" 

will give the device number 8 of the directory. If it differs between the 7 directory and its parent then you have a 6 mount point.

Add /. onto the directory name 5 if you want symlinks to different filesystems 4 to count as mountpoints (you'll always want 3 it for the parent).

Disadvantages: uses GNU 2 find so less portable

Advantages: Reports 1 mount points not recorded in /etc/mtab.

Score: 3
if mount | cut -d ' ' -f 3 | grep '^/mnt/disk$' > /dev/null ; then

EDIT: Used Bombe's idea to use cut.


Score: 3
df $path_in_question | grep " $path_in_question$"

This will set $? upon completion.


Score: 3

Unfortunately both mountpoint and stat will 8 have the side-effect of MOUNTING the directory you 7 are testing if you are using automount. Or 6 at least it does for me on Debian using 5 auto cifs to a WD MyBookLive networked disk. I 4 ended up with a variant of the /proc/mounts 3 made more complex because each POTENTIAL mount is 2 already in /proc/mounts even if its not 1 actually mounted!

cut -d ' ' -f 1 < /proc/mounts | grep -q '^//disk/Public$' && umount /tmp/cifs/disk/Public
   'disk' is the name of the server (networked disk) in /etc/hosts.
   '//disk/Public' is the cifs share name
   '/tmp/cifs' is where my automounts go (I have /tmp as RAM disk and / is read-only)
   '/tmp/cifs/disk' is a normal directory created when the server (called 'disk') is live.
   '/tmp/cifs/disk/Public' is the mount point for my 'Public' share.
Score: 2
for mountedPath in `mount | cut -d ' ' -f 3`; do
    if [ "${mountedPath}" == "${wantedPath}" ]; then
        exit 0
exit 1


Score: 0

Here is a variant with "df -P" which is 1 supposed to be portable:

mat@owiowi:/tmp$ f(){ df -P  | awk '{ if($6 == "'$1'")print   }' ; }
mat@owiowi:/tmp$ f /
/dev/mapper/lvm0-vol1  20642428  17141492   2452360      88% /
mat@owiowi:/tmp$ f /mnt
mat@owiowi:/tmp$ f /mnt/media
/dev/mapper/lvm0-media  41954040  34509868   7444172      83% /mnt/media
Score: 0
mount | awk '$3 == "/pa/th" {print $1}'

Empty if is not a mountpoint ^^


Score: 0

stat --printf '%m' shows the mount point of a given file or 6 directory.

realpath converts relative paths to direct.

Comparing 5 the results of the two will tell you if 4 a directory is a mount point. stat is very 3 portable. realpath is less so, but it is only needed 2 if you want to check relative paths.

I'm 1 not sure how portable mountpoint is.

if [ "$(stat --printf '%m' "${DIR}")" = "$(realpath "${DIR}")" ]; then
    echo "This directory is a mount point."
    echo "This is not a mount point."

Without realpath:

if [  "${DIR}" = "$(stat --printf '%m' "${DIR}")" ]; then
    echo "This directory is a mount point."
    echo "This is not a mount point."

More Related questions