How to Check if a Volume is Mounted in Bash

0 min read 136 words

If you need to check if a volume is mounted in a Bash script, then you can do the following.

How to Check Mounted Volumes

First we need to determine the command that will be able to check.

This can be done with the /proc/mounts path.

How to Check if a Volume is Mounted in Bash

if grep -qs '/mnt/foo ' /proc/mounts; then
    echo "It's mounted."
else
    echo "It's not mounted."
fi

How to Check if a Volume is Mounted and Available

MNT_DIR=/mnt/foo
df_result=$(timeout 10 df "$MNT_DIR")
[[ $df_result =~ $MNT_DIR ]] 
if [ "$BASH_REMATCH" = "$MNT_DIR" ]
then
    echo "It's available."
else
    echo "It's not available."
fi

Another way of Checking Volume Mounts

mount \
    | cut -f 3 -d ' ' \
    | grep -q /mnt/foo \
  && echo "mounted" || echo "not mounted"
Tags:
Andrew
Andrew

Andrew is a visionary software engineer and DevOps expert with a proven track record of delivering cutting-edge solutions that drive innovation at Ataiva.com. As a leader on numerous high-profile projects, Andrew brings his exceptional technical expertise and collaborative leadership skills to the table, fostering a culture of agility and excellence within the team. With a passion for architecting scalable systems, automating workflows, and empowering teams, Andrew is a sought-after authority in the field of software development and DevOps.

Tags