findmnt -rno SOURCE,TARGET "$1" avoids all the problems in the other answers. It cleanly does the job with just one command.
Other approaches have these downsides:
grep -q and grep -s are an extra unnecessary step and aren't supported everywhere.
/proc/\* isn't supported everywhere. (mountpoint is also based on proc).
mountinfo is based on /proc/..
cut -f3 -d' ' messes up spaces in path names
Parsing mount's white space is problematic. It's man page now says:
.. listing mode is maintained for backward compatibility only.
For more robust and customizable output use findmnt(8), especially in your scripts.
Bash functions:
#These functions return exit codes: 0 = found, 1 = not found
isDevMounted () { findmnt --source "$1" >/dev/null;} #device only
isPathMounted() { findmnt --target "$1" >/dev/null;} #path only
isMounted () { findmnt "$1" >/dev/null;} #device or path
#Usage examples:
if isDevMounted "/dev/sda10";
then echo "device is mounted"
else echo "device is not mounted"
fi
if isPathMounted "/mnt/C";
then echo "path is mounted"
else echo "path is not mounted"
fi
#Universal (device OR path):
if isMounted "/dev/sda10";
then echo "device is mounted"
else echo "device is not mounted"
fi
if isMounted "/mnt/C";
then echo "path is mounted"
else echo "path is not mounted"
fi
A script like this isn't ever going to be portable. A dirty secret in unix is that only the kernel knows what filesystems are where, and short of things like /proc (not portable) it'll never give you a straight answer.
I typically use df to discover what the mount-point of a subdirectory is, and what filesystem it is in.
For instance (requires posix shell like ash / AT&T ksh / bash / etc)
case $(df $mount)
in
$(df /)) echo $mount is not mounted ;;
*) echo $mount has a non-root filesystem mounted on it ;;
esac
the following is what i use in one of my rsync backup cron-jobs. it checks to see if /backup is mounted, and tries to mount it if it isn't (it may fail because the drive is in a hot-swap bay and may not even be present in the system)
NOTE: the following only works on linux, because it greps /proc/mounts - a more portable version would run 'mount | grep /backup', as in Matthew's answer..
if ! grep -q /backup /proc/mounts ; then
if ! mount /backup ; then
echo "failed"
exit 1
fi
fi
echo "suceeded."
# do stuff here
Since in order to mount, you need to have a directory there anyway, that gets mounted over, my strategy was always to create a bogus file with a strange filename that would never be used, and just check for it's existence. If the file was there, then nothing was mounted on that spot...
I don't think this works for mounting network drives or things like that. I used it for flash drives.
How about comparing devices numbers? I was just trying to think of the most esoteric way..
#!/bin/bash
if [[ $(stat -c "%d" /mnt) -ne $(stat -c "%d" /mnt/foo) ]]; then
echo "Somethin mounted there I reckon"
fi
There a flaw in my logic with that ...
As a Function:
#!/usr/bin/bash
function somethingMounted {
mountpoint="$1"
if ! device1=$(stat -c "%d" $mountpoint); then
echo "Error on stat of mount point, maybe file doesn't exist?" 1>&2
return 1
fi
if ! device2=$(stat -c "%d" $mountpoint/..); then
echo "Error on stat one level up from mount point, maybe file doesn't exist?" 1>&2
return 1
fi
if [[ $device1 -ne $device2 ]]; then
#echo "Somethin mounted there I reckon"
return 0
else
#echo "Nothin mounted it seems"
return 1
fi
}
if somethingMounted /tmp; then
echo "Yup"
fi
The echo error messages are probably redundant, because stat will display the an error as well.
This script will check if the drive is mounted and actually available. It can be written in more compact but I prefer to have it simple like this since I'm not that familiar with Bash. You may want to change the timeout, it is set to 10 sec. in the script.
MNT_DIR=/mnt/foo
df_result=$(timeout 10 df "$MNT_DIR")
[[ $df_result =~ $MNT_DIR ]]
if [ "$BASH_REMATCH" = "$MNT_DIR" ]
then
echo "It's available."
else
echo "It's not available."
fi
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
None of these satisfy the use case where a given directory is a sub directory within another mount point. For example, you might have /thing which is an NFS mount to host:/real_thing. Using grep for this purpose on /proc/mounts /etc/mtab or 'mount' will not work, because you will be looking for a mount point that doesn't exist. For example, /thing/thingy is not a mount point, but /thing is mounted on host:/real_thing. The best answer voted on here is actually NOT "the best way to determine if a directory/volumne is mounted". I'd vote in favour using 'df -P' (-P POSIX standards mode) as a cleaner strategy:
This is all very useful if you are trying to create some sort of chroot that mirrors mount points outside of the chroot, within the chroot, via some arbitrary directory or file list.
Avoid using
/etc/mtab
because it may be inconsistent.Avoid piping
mount
because it needn't be that complicated.Simply:
(The space after the
/mnt/foo
is to avoid matching e.g./mnt/foo-bar
.)or
findmnt -rno SOURCE,TARGET "$1"
avoids all the problems in the other answers. It cleanly does the job with just one command.Other approaches have these downsides:
grep -q
andgrep -s
are an extra unnecessary step and aren't supported everywhere./proc/\*
isn't supported everywhere. (mountpoint
is also based on proc).mountinfo
is based on /proc/..cut -f3 -d' '
messes up spaces in path namesBash functions:
#Usage examples:
A script like this isn't ever going to be portable. A dirty secret in unix is that only the kernel knows what filesystems are where, and short of things like /proc (not portable) it'll never give you a straight answer.
I typically use df to discover what the mount-point of a subdirectory is, and what filesystem it is in.
For instance (requires posix shell like ash / AT&T ksh / bash / etc)
Kinda tells you useful information.
the following is what i use in one of my rsync backup cron-jobs. it checks to see if /backup is mounted, and tries to mount it if it isn't (it may fail because the drive is in a hot-swap bay and may not even be present in the system)
NOTE: the following only works on linux, because it greps /proc/mounts - a more portable version would run 'mount | grep /backup', as in Matthew's answer..
Since in order to mount, you need to have a directory there anyway, that gets mounted over, my strategy was always to create a bogus file with a strange filename that would never be used, and just check for it's existence. If the file was there, then nothing was mounted on that spot...
I don't think this works for mounting network drives or things like that. I used it for flash drives.
How about comparing devices numbers? I was just trying to think of the most esoteric way..
There a flaw in my logic with that ...
As a Function:
The echo error messages are probably redundant, because stat will display the an error as well.
This script will check if the drive is mounted and actually available. It can be written in more compact but I prefer to have it simple like this since I'm not that familiar with Bash. You may want to change the timeout, it is set to 10 sec. in the script.
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
)None of these satisfy the use case where a given directory is a sub directory within another mount point. For example, you might have /thing which is an NFS mount to host:/real_thing. Using grep for this purpose on /proc/mounts /etc/mtab or 'mount' will not work, because you will be looking for a mount point that doesn't exist. For example, /thing/thingy is not a mount point, but /thing is mounted on host:/real_thing. The best answer voted on here is actually NOT "the best way to determine if a directory/volumne is mounted". I'd vote in favour using 'df -P' (-P POSIX standards mode) as a cleaner strategy:
The output from running this will be:
If you want to know what the real mount point is, no problem:
The output from that command will be:
This is all very useful if you are trying to create some sort of chroot that mirrors mount points outside of the chroot, within the chroot, via some arbitrary directory or file list.