Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

bash

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:

#!/bin/bash

# Local mount point
LOCALMOUNTPOINT="/folder/share"

# Perform the mount if it does not already exist
if ...
then
/sbin/mount -t smbfs //user:password@serveraddress/share $LOCALMOUNTPOINT

else
    echo "Already mounted"
fi
like image 255
fredrik Avatar asked Mar 05 '14 08:03

fredrik


People also ask

How do I know if my path is mounted?

Using the mount Command One way we can determine if a directory is mounted is by running the mount command and filtering the output. The above line will exit with 0 (success) if /mnt/backup is a mount point. Otherwise, it'll return -1 (error).

How can I see what is mounted in Linux?

You need to use any one of the following command to see mounted drives under Linux operating systems. [a] df command – Shoe file system disk space usage. [b] mount command – Show all mounted file systems. /proc/mounts or /proc/self/mounts file – Show all mounted file systems.

How can I see what directory A directory is mounted in Linux?

The findmnt command is a simple command-line utility used to display a list of currently mounted file systems or search for a file system in /etc/fstab, /etc/mtab or /proc/self/mountinfo.

What is mount in bash?

The mount command mounts a storage device or filesystem, making it accessible and attaching it to an existing directory structure. The umount command "unmounts" a mounted filesystem, informing the system to complete any pending read or write operations, and safely detaching it.


2 Answers

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

LOCALMOUNTPOINT="/folder/share"

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

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.

like image 96
ysakamoto Avatar answered Oct 22 '22 00:10

ysakamoto


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"
like image 44
daniel Avatar answered Oct 22 '22 00:10

daniel