112

I am using

mount -o bind /some/directory/here /foo/bar

I want to check /foo/bar though with a bash script, and see if its been mounted? If not, then call the above mount command, else do something else. How can I do this?

CentOS is the operating system.

Mateusz Piotrowski
  • 8,029
  • 10
  • 53
  • 79
Justin
  • 42,716
  • 77
  • 201
  • 296
  • something like "if [[ $(mount -l ... | fgrep ... | wc -l) ]]; then ..." – bobah Feb 23 '12 at 22:51
  • Similar questions are on [Server Fault](//serverfault.com/q/50585), **Stack Overflow** and [Unix & Linux Stack Exchange](//unix.stackexchange.com/q/38870). – Sasha Aug 11 '19 at 22:05

8 Answers8

202

You didn't bother to mention an O/S.

Ubuntu Linux 11.10 (and probably most up-to-date flavors of Linux) have the mountpoint command.

Here's an example on one of my servers:

$ mountpoint /oracle
/oracle is a mountpoint
$ mountpoint /bin
/bin is not a mountpoint

Actually, in your case, you should be able to use the -q option, like this:

mountpoint -q /foo/bar || mount -o bind /some/directory/here /foo/bar
starball
  • 20,030
  • 7
  • 43
  • 238
Mark J. Bobak
  • 13,720
  • 6
  • 39
  • 67
  • 1
    mountpoint /foo/bar says its not a mountpoint, but in fact it is. Very strange. – Justin Feb 23 '12 at 22:55
  • 7
    You actually have something mounted at the time you ran the 'mountpoint' command? Even if it's intended to be a mountpoint, unless it *actually* has something mounted, it's just a directory. – Mark J. Bobak Feb 23 '12 at 22:57
  • 2
    +1, works on Ubuntu 10.10 as well. [It's been in Ubuntu since 8.04](http://packages.ubuntu.com/hardy/i386/initscripts/filelist). – l0b0 Feb 24 '12 at 12:04
  • 3
    mountpoint is present in RHEL 6.3 too (probably even in earlier versions) – Joril Jan 16 '13 at 16:28
  • 1
    @Justin It seems that `mountpoint` will say that directory is not mountpoint if binded directory and the original directory are on the same device (see http://goo.gl/Hs7QKV). – smbear May 06 '14 at 07:05
  • 1
    Works perfectly on Suse Linux Enterprise Server 11 (SLES11SP2) – Rom098 May 07 '14 at 12:10
  • 2
    mountpoint is present and works on Debian 7 ("Wheezy") and UCS 4 perfectly, too – Lahmizzar May 25 '15 at 00:11
  • 8
    As mentioned in [another comment](http://stackoverflow.com/questions/479226/how-to-test-if-a-given-path-is-a-mount-point#comment15969742_483472) mountpoint doesn't work with bind mounts. The snippet will mount the dir multiple times – csanchez Jun 10 '15 at 09:36
  • 1
    Does mountpoint tell me whether this directory can be mounted (i.e. is in /etc/fstab) or whether it is currently mounted? – sid-kap Dec 22 '15 at 17:10
  • It only indicates whether there is *currently* something mounted. – Mark J. Bobak Dec 23 '15 at 18:29
  • would not work on a lot of embedded systems where mountpoint is missing – Angelo Dureghello Aug 25 '20 at 13:28
77

Running the mount command without arguments will tell you the current mounts. From a shell script, you can check for the mount point with grep and an if-statement:

if mount | grep /mnt/md0 > /dev/null; then
    echo "yay"
else
    echo "nay"
fi

In my example, the if-statement is checking the exit code of grep, which indicates if there was a match. Since I don't want the output to be displayed when there is a match, I'm redirecting it to /dev/null.

Eliran Malka
  • 15,821
  • 6
  • 77
  • 100
Christopher Neylan
  • 8,018
  • 3
  • 38
  • 51
  • 4
    You don't need to check if it returns a string. `grep` returns a non-zero exit status if there are no matches. There is no need for the test, or the subshell created by $(). `if command | grep -q 'stuff'; then ...` – jordanm Feb 23 '12 at 23:54
  • 33
    **This does not work with mount bind and therefore should not be accepted as the answer. This answer should be voted down.** You cannot depend on `mount | grep ...` showing you that your mount bind is still active. If the underlying device is unmounted and remounted, the mount bind will actually no longer connect the two files/directories even though `mount` still shows it's connected. And if you do `umount ...` it will even tell you it's not mounted, although `mount` said it was. HUGE flaw in linux file system. – Dev Null Dec 03 '13 at 16:00
  • @DevNull: I'am having exactly the issue you've described. Any solution to this problem? – Isaac Dec 06 '14 at 05:14
  • @Isaac, sorry for the delay in response. I would try using `readlink -f`, which returns nothing when the path/file doesn't exist. If the underlying /mount/path/to_file isn't there (per readlink), the underlying mount is broke (remount the underlying mount and the binding). If the underlying file is there and `readlink -f` says the binding-version of that file is not there, the binding is broken, just remount the binding. – Dev Null Dec 29 '14 at 20:58
  • @DevNull not work readlink -f also. readlink -f /path/not_mounted/ show path on proxmox NFS unmounted filesystem type NAS. – abkrim Mar 24 '15 at 06:00
  • @abkrim, how about `stat -q "/path/to/mount/some_file"; echo $?` ?? it should be 0 if the file is there and 1 if not. – Dev Null Mar 25 '15 at 15:06
  • 2
    If there is a mount called `/mnt/md0xxx` so the command will indicate `/mnt/md0` as mounted while what it does not have to be... – David L. Jul 01 '16 at 21:07
  • I use this on VM's for easy check of mounted NFS shares: #!/bin/bash RED='\033[0;31m' GREEN='\033[0;32m' if [ -e /var/www/@Recycle ] then echo -e "[ ${GREEN}OK \e[0m ] NFS | WWW" >/dev/tty1 NC='\033[0m' # No Color else echo -e "[ ${RED}ERROR \e[0m ] NFS | WWW" >/dev/tty1 NC='\033[0m' # No Color fi – SamTzu Apr 24 '18 at 14:57
  • This answer is plainly wrong because it will falsely indicate that a directory is mounted if its name or device path is a substring of another path that is mounted. – weaver Jan 12 '21 at 21:58
65

The manual of mountpoint says that it:

checks whether the given directory or file is mentioned in the /proc/self/mountinfo file.

The manual of mount says that:

The listing mode is maintained for backward compatibility only. For more robust and customizable output use findmnt(8), especially in your scripts.

So the correct command to use is findmnt, which is itself part of the util-linux package and, according to the manual:

is able to search in /etc/fstab, /etc/mtab or /proc/self/mountinfo

So it actually searches more things than mountpoint. It also provides the convenient option:

-M, --mountpoint path

Explicitly define the mountpoint file or directory. See also --target.

In summary, to check whether a directory is mounted with bash, you can use:

if [[ $(findmnt -M "$FOLDER") ]]; then
    echo "Mounted"
else
    echo "Not mounted"
fi

Example:

mkdir -p /tmp/foo/{a,b}
cd /tmp/foo

sudo mount -o bind a b
touch a/file
ls b/ # should show file
rm -f b/file
ls a/ # should show nothing

[[ $(findmnt -M b) ]] && echo "Mounted"
sudo umount b
[[ $(findmnt -M b) ]] || echo "Unmounted"
Jonathan H
  • 7,591
  • 5
  • 47
  • 80
2

My solution:

is_mount() {
    path=$(readlink -f $1)
    grep -q "$path" /proc/mounts
}

Example:

is_mount /path/to/var/run/mydir/ || mount --bind /var/run/mydir/ /path/to/var/run/mydir/

For Mark J. Bobak's answer, mountpoint not work if mount with bind option in different filesystem.

For Christopher Neylan's answer, it's not need to redirect grep's output to /dev/null, just use grep -q instead.

The most important, canonicalize the path by using readlink -f $mypath:

  • If you check path such as /path/to/dir/ end with backslash, the path in /proc/mounts or mount output is /path/to/dir
  • In most linux release, /var/run/ is the symlink of /run/, so if you mount bind for /var/run/mypath and check if it mounted, it will display as /run/mypath in /proc/mounts.
Tanky Woo
  • 4,906
  • 9
  • 44
  • 75
  • 3
    I think "grep -q "$path " /proc/mounts" (with space) is even better... Otherwise is_mount ab will return true if abc is mounted?! – alfonx Oct 17 '18 at 12:24
2

I like the answers that use /proc/mounts, but I don't like doing a simple grep. That can give you false positives. What you really want to know is "do any of the rows have this exact string for field number 2". So, ask that question. (in this case I'm checking /opt)

awk -v status=1 '$2 == "/opt" {status=0} END {exit status}' /proc/mounts

# and you can use it in and if like so:

if awk -v status=1 '$2 == "/opt" {status=0} END {exit status}' /proc/mounts; then
  echo "yes"
else
  echo "no"
fi
Bruno Bronosky
  • 66,273
  • 12
  • 162
  • 149
  • 1
    This is lovely! Precisely what you want for a small embedded system where no flashy tools like `mountpoint` or `findmnt` exist. Very simple reply, much joy, such UNIX :) – troglobit Jan 27 '20 at 22:41
1

The answers here are too complicated just check if the mount exists using:

cat /proc/mounts | tail -n 1

This only outputs the last mounted folder, if you want to see all of them just remove the tail command.

Gabriel Fair
  • 4,081
  • 5
  • 33
  • 54
0

Another clean solution is like that:

$ mount | grep /dev/sdb1 > /dev/null && echo mounted || echo unmounted

For sure, 'echo something' can be substituted by whatever you need to do for each case.

Hudson Santos
  • 155
  • 10
-5

In my .bashrc, I made the following alias:

alias disk-list="sudo fdisk -l"
  • the request is about script and it is best to avoid sudo and program which target is to "manipulate disk partition" – bcag2 Jan 22 '20 at 07:56