1. Introduction

In this article, we’ll discuss different ways to determine if a directory is mounted. We’ll be doing so using the commands mount, mountpoint, and findmnt, and by reading /proc/mounts.

Then, we’ll also be making a small script in bash. As an example, we’ll check if /mnt/backup is mounted, and then we’ll mount it if it isn’t.

2. Using the mount Command

One way we can determine if a directory is mounted is by running the mount command and filtering the output.

It outputs a list of currently mounted filesystems, so we can use awk to search for the directory which is in column number 3:

$ mount | awk '{if ($3 == "/mnt/backup") { exit 0}} ENDFILE{exit -1}'

The above line will exit with 0 (success) if /mnt/backup is a mount point. Otherwise, it’ll return -1 (error).

The downside of this approach is that this won’t work properly when the folder’s name has whitespaces.

Let’s write an example to mount /mnt/backup unless it’s already mounted:

$ {
    if mount | awk '{if ($3 == "/mnt/backup") { exit 0}} ENDFILE{exit -1}'; then
        echo "/mnt/backup already mounted"
    else
        mount /dev/sdc1 /mnt/backup
    fi
} 

We can make this more portable by writing a function that takes the path to check as a parameter:

$ is_mounted() {
    mount | awk -v DIR="$1" '{if ($3 == DIR) { exit 0}} ENDFILE{exit -1}'
}

Now we can use that function to make the if-else more clear:

$ {
    if is_mounted "/mnt/backup"; then
        echo "/mnt/backup already mounted"
    else
        mount /dev/sdc1 /mnt/backup
    fi
}

And finally, if we are only interested in one result, we can write a one-liner. Let’s write a line using || to mount the directory if it isn’t mounted:

$ mount | awk '{if ($3 == "/mnt/backup") { exit 0}} ENDFILE{exit -1}' || mount /dev/sdc1 /mnt/backup

Alternatively, we should use && if we want to run a task only when the directory is indeed mounted:

$ mount | awk '{if ($3 == "/mnt/backup") { exit 0}} ENDFILE{exit -1}' && echo "/mnt/backup already mounted"

3. Using the mountpoint Command

We can also use the mountpoint command, which accepts a directory as an argument and prints if it’s mounted. This way, we can use directly mountpoint without awk:

$ mountpoint /mnt/backup

Let’s repeat the example from the previous section using mountpoint. It also accepts the parameter -q to be quiet, so we don’t need to redirect the output to /dev/null:

$ {
    if mountpoint -q /mnt/backup; then
        echo "/mnt/backup already mounted"
    else
        mount /dev/sdc1 /mnt/backup
    fi
}

The code is clear, although we can parameterize the directory and write a function as we did with mount:

$ is_mounted() {
    mountpoint -q "$1"
}

Then again, we can write it all in one line. We have two alternatives — one in case the folder is not mounted, and one when it is:

$ mountpoint -q /mnt/backup || mount /dev/sdc1 /mnt/backup
$ mountpoint -q /mnt/backup && echo "/mnt/backup already mounted"

4. Using the findmnt Command

Another way to determine if a directory is mounted is by using findmnt and giving the directory as the parameter.

findmnt displays a tree of all mounted filesystems.

However, if we give it the directory as a parameter, it prints only the information about that directory:

$ findmnt /mnt/backup

Let’s repeat the example using findmnt inside an if-else:

$ {
    if findmnt /mnt/backup >/dev/null; then
        echo "/mnt/backup already mounted"
    else
        mount /dev/sdc1 /mnt/backup
    fi
}

We can also rewrite our is_mounted() function:

$ is_mounted() {
    findmnt "$1" >/dev/null
}

And once again, we can have a handy one-liner for both cases:

$ findmnt /mnt/backup >/dev/null || mount /dev/sdc1 /mnt/backup
$ findmnt /mnt/backup >/dev/null && echo "/mnt/backup already mounted"

5. Reading /proc/mounts

Finally, we can check if a directory is mounted if we filter the content of the /proc/mounts file. This file has similar information as mount and findmnt, so let’s use awk and search for the desired directory:

$ awk '{if ($2 == "/mnt/backup") { exit 0}} ENDFILE{exit -1}' < /proc/mounts

Now, we can redo the if-else example:

$ {
    if awk '{if ($2 == "/mnt/backup") { exit 0}} ENDFILE{exit -1}' < /proc/mounts; then
        echo "/mnt/backup already mounted"
    else
        mount /dev/sdc1 /mnt/backup
    fi
}

Let’s make the code clearer by writing the condition inside a function:

$ is_mounted() {
    awk -v DIR="$1" '{if ($2 == DIR) { exit 0}} ENDFILE{exit -1}' < /proc/mounts
}

And finally, we can use the one-liner alteratives:

$ awk '{if ($2 == "/mnt/backup") { exit 0}} ENDFILE{exit -1}' < /proc/mounts || mount /dev/sdc1 /mnt/backup
$ awk '{if ($2 == "/mnt/backup") { exit 0}} ENDFILE{exit -1}' < /proc/mounts && echo "/mnt/backup already mounted"

As mentioned in the mount example, this method won’t work well when the directory’s name has whitespaces.

6. Conclusion

In this article, we saw four different ways we can check if a directory is mounted, and we wrote some small bash utilities to help with that.


» 下一篇: cd - 用法