Skip to content

How to Check if a Volume is Mounted in Bash

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
Code language: Bash (bash)

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
Code language: Bash (bash)

Another way of Checking Volume Mounts

mount \ | cut -f 3 -d ' ' \ | grep -q /mnt/foo \ && echo "mounted" || echo "not mounted"
Code language: Bash (bash)

See also  How to remove files without a certain extension?
Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x