How to check if filepath is mounted in OS X using bash?


Question or issue on macOS:

How do I perform the mount only if it has not already been mounted?

This is on OS X 10.9 and this is what I have currently:


# Local mount point

# Perform the mount if it does not already exist
if ...
/sbin/mount -t smbfs //user:[email protected]/share $LOCALMOUNTPOINT

    echo "Already mounted"

How to solve this problem?

Solution no. 1:

While @hd1’s answer gives you whether the file exists, it does not necessary mean that the directory is mounted or not. It is possible that the file happen to exist if you use this script for different machines or use different mount points. I would suggest this


if mount | grep "on $LOCALMOUNTPOINT" > /dev/null; then
    echo "mounted"
    echo "not mounted"

Note that I include “on” in grep statement based on what mount command outputs in my machine. You said you use MacOS so it should work, but depending on what mount command outputs, you may need to modify the code above.

Solution no. 2:

This is what I use in my shell scripts on OS X 10.7.5

df | awk '{print $6}' | grep -Ex "/Volumes/myvolume"

For OS X 10.10 Yosemite I have to change to:

df | awk '{print $9}' | grep -Ex "/Volumes/myvolume"

Solution no. 3:

For me from this solution in stackoverflow_QA_Check if a directory exists in a shell script

[ -d /Volumes/ ] && echo "Already mounted /Volumes/ in OS X" || echo "Not mounted"

to use that

# 2 lines
$LOCALMOUNTPOINT="/folder/share" ;
[ -d $LOCALMOUNTPOINT ] && echo "Already mounted $LOCALMOUNTPOINT in OS X" || /sbin/mount -t smbfs //user:[email protected]/share $LOCALMOUNTPOINT

Here is Stackoverflow_QA_how-can-i-mount-an-smb-share-from-the-command-line
additional link about smb which I don’t know well

# Here is my solution
$LOCALMOUNTPOINT="/folder/share" ;
[ ! -d $LOCALMOUNTPOINT ] && mkdir $LOCALMOUNTPOINT && /sbin/mount -t smbfs //user:[email protected]/share $LOCALMOUNTPOINT || echo already mounted $LOCALMOUNTPOINT

I have to make new folder for my SMOOTH use before mounting that.

so I added mkdir $LOCALMOUNTPOINT

Thank you I learned a lot with this case.
Have A nice day!!! 😉

Hope this helps!