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"