Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to test if a given path is a mount point

Tags:

bash

shell

unix

Suppose do you want test if /mnt/disk is a mount point in a shell script. How do you do this?

like image 556
Andrea Francia Avatar asked Jan 26 '09 09:01

Andrea Francia


People also ask

How do you check if a path is mounted or not?

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 check mount point in Linux?

See mount points using the mount command:If followed by the -l flag, it will also show the mount point name; the output is similar to the mount command without flags.

How do you check the mount points in Windows?

In Disk Manager, right-click the partition or volume that has the folder in which you want to mount the drive. Click Change Drive Letter and Paths and then click Add. Click Mount in the following empty NTFS folder. Type the path to an empty folder on an NTFS volume, or click Browse to locate it.

What is a mount path?

Mount Path. The path to which the data is written to and read from. Free Space. Not available for cloud storage. Size on Disk.


2 Answers

Not relying on mount, /etc/mtab, /proc/mounts, etc.:

if [ `stat -c%d "$dir"` != `stat -c%d "$dir/.."` ]; then     echo "$dir is mounted" else     echo "$dir is not mounted" fi 

When $dir is a mount point, it has a different device number than its parent directory.

The benefit over the alternatives listed so far is that you don't have to parse anything, and it does the right thing if dir=/some//path/../with///extra/components.

The downside is that it doesn't mark / as a mountpoint. Well, that's easy enough to special-case, but still.

like image 32
ephemient Avatar answered Sep 17 '22 14:09

ephemient


I discover that on my Fedora 7 there is a mountpoint command.

From man mountpoint:

NAME        mountpoint - see if a directory is a mountpoint  SYNOPSIS        /bin/mountpoint [-q] [-d] /path/to/directory        /bin/mountpoint -x /dev/device 

Apparently it come with the sysvinit package, I don't know if this command is available on other systems.

[root@myhost~]# rpm -qf $(which mountpoint) sysvinit-2.86-17 
like image 77
Andrea Francia Avatar answered Sep 19 '22 14:09

Andrea Francia