Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Check if directory mounted with bash

I am using

mount -o bind /some/directory/here /foo/bar 

I want to check /foo/bar though with a bash script, and see if its been mounted? If not, then call the above mount command, else do something else. How can I do this?

CentOS is the operating system.

like image 836
Justin Avatar asked Feb 23 '12 22:02

Justin


People also ask

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. 1.

How do you check if a device is mounted on 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.

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.

How do I know if my LVM is mounted?

You can get LV information by using the lvdisplay command. If you have any logical volumes they will appear as such as well as additional information about that volume such as the path, logical volume name, volume group name, size, etc.


1 Answers

You didn't bother to mention an O/S.

Ubuntu Linux 11.10 (and probably most up-to-date flavors of Linux) have the mountpoint command.

Here's an example on one of my servers:

$ mountpoint /oracle /oracle is a mountpoint $ mountpoint /bin /bin is not a mountpoint 

Actually, in your case, you should be able to use the -q option, like this:

mountpoint -q /foo/bar || mount -o bind /some/directory/here /foo/bar 

Hope that helps.

like image 91
Mark J. Bobak Avatar answered Oct 01 '22 19:10

Mark J. Bobak