Shell script to know whether a filesystem is already mounted

You can check the type of the filesystem.

$ stat -f -c '%T' /
xfs
$ stat -f -c '%T' /dev/shm
tmpfs

You could also check whether a directory is a mountpoint by comparing its device with its parent's.

$ stat -c '%D' /
901
$ stat -c '%D' /home
fe01
$ stat -c '%D' /home/$USER
fe01

There's a tool specifically for this: mountpoint(1)

if mountpoint -q "$directory" ; then
    echo it is a mounted mountpoint
else
    echo it is not a mounted mountpoint
fi

And you don't even have to scrape strings to do it!

Note that I find this tool in Debian's initscripts package. How available it is elsewhere is not something I can comment on.


Something like this, while hackish, should do the trick:

FS_TO_CHECK="/dev" # For example... change this to suit your needs.

if grep -F " $FS_TO_CHECK " /proc/mounts > /dev/null; then
    # Filesystem is mounted
else
    # Filesystem is not mounted
fi