How to test if a given path is a mount point

2019-03-08 18:26发布

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

标签: bash unix shell
10条回答
虎瘦雄心在
2楼-- · 2019-03-08 18:48
mount | awk '$3 == "/pa/th" {print $1}'

Empty if is not a mountpoint ^^

查看更多
干净又极端
3楼-- · 2019-03-08 18:51

Using GNU find

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

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

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

Disadvantages: uses GNU find so less portable

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

查看更多
一夜七次
4楼-- · 2019-03-08 18:51

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

realpath converts relative paths to direct.

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

I'm not sure how portable mountpoint is.

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

Without realpath:

if [  "${DIR}" = "$(stat --printf '%m' "${DIR}")" ]; then
    echo "This directory is a mount point."
else
    echo "This is not a mount point."
fi
查看更多
我欲成王,谁敢阻挡
5楼-- · 2019-03-08 18:59

Here is a variant with "df -P" which is 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
查看更多
趁早两清
6楼-- · 2019-03-08 19:01

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
查看更多
疯言疯语
7楼-- · 2019-03-08 19:01

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

cut -d ' ' -f 1 < /proc/mounts | grep -q '^//disk/Public$' && umount /tmp/cifs/disk/Public
Where
   '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.
查看更多
登录 后发表回答