41

How to check whether or not a particular directory is a mount point? For instance there is a folder named /test that exists, and I want to check if it is a mount point or not.

miken32
  • 466
unixx
  • 421
  • 6
    You don't mount directories on Linux. You mount devices to particular directories. Checking to see if something is mounted is as simple as looking at the output of the mount command. – HalosGhost Aug 21 '14 at 14:01
  • 5
    Let's help, perhaps they do not understand this finer point! – slm Aug 21 '14 at 14:02
  • 4
    The answers below will show you how to examine the mount table, but a simpler solution is to create a file in the mount point directory before anything is mounted on it. Call it anything you like, but one example is NOTMOUNTED. When you can see the file, the directory is not a mount point, and when you don't, it is. – Mark Plotnick Aug 21 '14 at 14:13

6 Answers6

48

If you want to check it's the mount point of a file system, that's what the mountpoint command (on most Linux-based systems) is for:

if mountpoint -q -- "$dir"; then
  printf '%s\n' "$dir is a mount point"
fi

It does that by checking whether . and .. have the same device number (st_dev in stat() result). So if you don't have the mountpoint command, you could do:

perl -le '$dir = shift; exit(1) unless
  (@a = stat "$dir/." and @b = stat "$dir/.." and
  ($a[0] != $b[0] || $a[1] == $b[1]))' "$dir"

Like mountpoint, it will return true for / even if / is not a mount point (like when in a chroot jail), or false for a mount point of a bind mount of the same file system within itself.

Contrary to mountpoint, for symbolic links, it will check whether the target of the symlink is a mountpoint.

  • 1
    At least on Ubuntu, the output of mountpoint already says "$dir is a mountpoint" ,so you don't need the if part around it. – Sergiy Kolodyazhnyy Sep 17 '16 at 09:49
  • @SergiyKolodyazhnyy the -q means quiet output, so in this case, the output is being suppressed and then replaced by custom output. Unnecessary, but yeah. – Wyrmwood Jul 25 '17 at 19:49
  • @Wyrmwood I know what the options are. What I'm saying is that mountpoint "$dir" already does exactly the same as the 3-line if statement does here. Functionally they're the same. Such use of mountpoint -q in if statement can be used when you want to perform some action based on the exit status, but for printing the message to user its unnecessary - it's already the default behavior of the program. – Sergiy Kolodyazhnyy Jul 25 '17 at 20:21
  • How can I install mountpoint into macOS? I have tried through brew but system is unable to see it – alper Apr 14 '22 at 12:35
23

As HalosGhost mentions in the comments, directories aren't necessarily mounted per se. Rather they're present on a device which has been mounted. To check for this you can use the df command like so:

$ df -h /boot/
Filesystem            Size  Used Avail Use% Mounted on
/dev/hda1              99M   55M   40M  59% /boot

Here we can see that the directory /boot is part of the filesystem, /dev/hda1. This is a physical device, on the system, a HDD.

You can also come at this a little bit differently by using the mount command to query the system to see what devices are currently mounted:

$ mount | column -t
/dev/mapper/VolGroup00-LogVol00  on  /                         type  ext3         (rw)
proc                             on  /proc                     type  proc         (rw)
sysfs                            on  /sys                      type  sysfs        (rw)
devpts                           on  /dev/pts                  type  devpts       (rw,gid=5,mode=620)
/dev/hda1                        on  /boot                     type  ext3         (rw)
tmpfs                            on  /dev/shm                  type  tmpfs        (rw)
/dev/mapper/lvm--raid-lvm0       on  /export/raid1             type  ext3         (rw)
none                             on  /proc/sys/fs/binfmt_misc  type  binfmt_misc  (rw)
sunrpc                           on  /var/lib/nfs/rpc_pipefs   type  rpc_pipefs   (rw)
nfsd                             on  /proc/fs/nfsd             type  nfsd         (rw)

Here you can see the type of device and the type of filesystems are currently mounted on your system. The 3rd column shows where they're mounted on the system within its filesystem.

slm
  • 369,824
  • 3
    To check if a particular directory is an active mountpoint (especially in scripts, where a binary result is handy), the mountpoint command is a nice alternative e.g. if mountpoint -q /path/to/dir; then .... I'm not sure how widely available it is though. – steeldriver Aug 21 '14 at 14:14
  • @steeldriver - cool, never seen that one before. It's present on my Red Hat distros. – slm Aug 21 '14 at 14:20
  • @slm: Am I going blind, or was your cat walking on your keyboard when you posted this answer?  It says, “Here we can see that the directory /usr is part of the filesystem, …”, but /usr appears nowhere else in the answer. – G-Man Says 'Reinstate Monica' Dec 06 '15 at 23:54
  • @G-Man - I meant to type /boot there, I fixed it. Given the above example shows /boot it seemed pretty obvious what I meant when I typed that, even with the typo. – slm Dec 07 '15 at 03:17
  • Why not just mounpoint $(pwd) ? It prints /jail/bin is a mountpoint. Btw, I think findmnt of @xharx 's answer is the best. – Rick May 25 '22 at 07:16
15

I was looking for the same question when I wanted to check mounting a new XFS filesystem.

I found the command findmnt: findmnt /directoryname

[root@CentOS7-Server /]# findmnt /mnt
TARGET SOURCE    FSTYPE OPTIONS
/mnt   /dev/sdb1 xfs    rw,relatime,seclabel,attr2,inode64,noquota
[root@CentOS7-Server /]# 
Stefan M
  • 1,616
xharx
  • 163
  • 2
  • 7
  • very concise solution! – Tung May 11 '21 at 22:07
  • This is the best. findmnt gives the most informative output compared to some others. That is, it tells which specific diretory is the target mounted from. One can run findmnt with no arguments. – Rick May 25 '22 at 07:15
9

Well, as others said you should edit your question and make it clear on what you are trying to achieve. As far as I understood, you need to check if a directory is mounted to a particular device. You can try something like below as well.

df -P /test | tail -1 | cut -d' ' -f 1

So basically, the above command lets you know the mount point of the directory if at all the device is mounted to a directory.

Ramesh
  • 39,297
3

Nice and short Python one-liner can be constructed based on Gilles' answer:

$ python -c 'import os,sys;print(os.path.ismount(sys.argv[1]))' /mnt/HDD
True

$ python -c 'import os,sys;print(os.path.ismount(sys.argv[1]))' /etc                
False

I've made custom implementation of mountpoint command in Python, which parses /proc/self/mounts file. Kind of the same behavior as mount in Stephane's answer, except that command parses /proc/self/mountinfo. Usage is very simple: is_mountpoint.py /path/to/dir.

#!/usr/bin/env python3
from os import path
import sys

def main():

    if not sys.argv[1]:
       print('Missing a path')
       sys.exit(1)

    full_path = path.realpath(sys.argv[1])
    with open('/proc/self/mounts') as mounts:
       print
       for line in mounts:
           if full_path in line:
              print(full_path,' is mountpoint')
              sys.exit(0)
    print(full_path,' is not a mountpoint')
    sys.exit(1)

if __name__ == '__main__':
    main()

Test run:

$ python3 ./is_mountpoint.py /mnt/HDD                                          
/mnt/HDD  is mountpoint
$ lsblk | grep HDD
└─sdb6   8:22   0 405.3G  0 part /mnt/HDD
$ python3 ./is_mountpoint.py $HOME                                             
/home/xieerqi  is not a mountpoint
0

Your question is little bit confusing, but I guess, one of the ways to check that: mount |awk '{print $3}'| grep -w <your_directory>. If output is empty then no device mounted to the directory . If not empty then some device mounted to the directory. Other way is to use df <your_directory>. If last field equals to your directory name - than some device is mounted to it.

gena2x
  • 2,397