What's the best way to check if a volume is mounted in a Bash script?

What I'd really like is a method that I can use like this:

if <something is mounted at /mnt/foo> 
then
   <Do some stuff>
else
   <Do some different stuff>
fi
link|improve this question

I was just about to write a script to do this myself. My first thought is to get info out of /etc/mtab But I haven't thumbed through my bash books yet to see if there's a more direct way. – 3dinfluence Aug 5 '09 at 20:23
feedback

10 Answers

up vote 15 down vote accepted

Avoid using /etc/mtab because it may be inconsistent.

Avoid piping mount because it needn't be that complicated.

Simply:

if grep -qs '/mnt/foo' /proc/mounts; then
    echo "It's mounted."
else
    echo "It's not mounted."
fi
link|improve this answer
2  
Not to mention, a call to mount can hang if the mountpoint is wedged. – Chad Huneycutt Aug 5 '09 at 20:32
2  
Good for linux, not for freebsd or solaris. – chris Aug 5 '09 at 20:34
2  
This is true, chris. Although the question was tagged linux. – Dan Carley Aug 5 '09 at 20:38
2  
I guess this is a philosophical question -- should we attempt to make things portable if possible or should we just assume that all the world's running windows/linux and act accordingly? – chris Aug 5 '09 at 20:47
3  
Actually, you should test for '/mnt/foo ', ie. with a space or you might get a false positive if you had mounted a volume named eg. 'fooks'. I just got that issue with two mount points, 'lmde' and 'lmde-home'. – marlar Aug 12 '11 at 20:21
show 11 more comments
feedback

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

Kinda tells you useful information.

link|improve this answer
The question is tagged linux, so maybe it doesn't have to be portable – Rory Aug 10 '09 at 15:59
feedback

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
link|improve this answer
Upvoted as a good sanity checking alternative. – Dan Carley Aug 5 '09 at 21:11
feedback
if mountpoint -q /mnt/foo 
then
   echo "mounted"
else
   echo "not mounted"
fi

or

mountpoint -q /mnt/foo && echo "mounted" || echo "not mounted"
link|improve this answer
feedback

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.

link|improve this answer
ghetto, but I like it :) +1 – Chad Huneycutt Aug 5 '09 at 21:29
I never said it was pretty B-) – Brian Postow Nov 3 '09 at 22:06
feedback

grep /etc/mtab for your mount point maybe?

link|improve this answer
mtab can get out of date or simply not be updated by mount, such as when you use mount -n because / is read-only. – chris Aug 5 '09 at 20:33
I agree, but that seemed like the first place to start looking. – Ophidian Aug 5 '09 at 21:30
feedback

This?:

volume="/media/storage"
if mount|grep $volume; then
echo "mounted"
else
echo "not mounted"
if

From: An Ubuntu forum

link|improve this answer
feedback

Does it need to be any more complicated than:

`mount | cut -f 3 -d ' ' | grep -q /mnt/foo && echo "mounted" || echo "not mounted"`

?

link|improve this answer
feedback

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.

link|improve this answer
Actually, would probably have to check the exit status of stat first for each call to make sure the file is there ... not as novel as I thought :-( – Kyle Brandt Aug 5 '09 at 21:08
feedback

Although this is a Linux question, why not make it portable when it is easily done?

The manual page of grep says: Portable shell scripts should avoid both -q and -s and should redirect standard and error output to /dev/null instead.

So I propose the following solution:

if grep /mnt/foo /proc/mounts > /dev/null 2>&1; then
        echo "Mounted"
else
        echo "NOT mounted"
fi
link|improve this answer
Many UNIX systems do not provide the /proc filesystem – Dmitri Chubarov Apr 27 at 19:09
feedback

Your Answer

 
or
required, but never shown

Not the answer you're looking for? Browse other questions tagged or ask your own question.