How to Check If a Volume Is Mounted in Bash: Reliable Methods & Examples


3 views

When automating system administration tasks, you often need to verify whether a specific directory contains a mounted filesystem before performing operations. This is especially critical when dealing with:

  • Network shares (NFS, SMB)
  • Removable media (USB drives)
  • Cloud storage mounts (s3fs, gcsfuse)
  • Container volumes

The most straightforward approach uses the mountpoint utility (part of util-linux package):

if mountpoint -q /mnt/foo; then
    echo "/mnt/foo is mounted"
    # Your operations here
else
    echo "/mnt/foo is not mounted"
    # Fallback operations
fi

Key advantages:

  • Explicitly designed for this purpose
  • Returns proper exit codes (0=success if mounted)
  • Works with all filesystem types

For systems without mountpoint command:

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

Pro tip: Add ^/dev/ to the grep pattern if you specifically want device mounts.

Legacy systems might need this approach:

if mount | grep -qs '/mnt/foo'; then
    echo "/mnt/foo is mounted"
else
    echo "/mnt/foo is not mounted"
fi

For more sophisticated checks:

# Check specific filesystem type
if findmnt -n -o FSTYPE -T /mnt/foo | grep -q '^ext4$'; then
    echo "/mnt/foo is ext4 filesystem"
fi

# Verify read-write status
if findmnt -no OPTIONS /mnt/foo | grep -q 'rw,'; then
    echo "/mnt/foo is writable"
fi
  • Directory existence ≠ mounted status (always verify)
  • Bind mounts require special handling
  • Network mounts may appear hung
  • Symbolic links to mount points can cause false positives

Here's a robust function for your scripts:

is_mounted() {
    local path=$1
    mountpoint -q "$path" 2>/dev/null || 
        grep -qs "$path" /proc/mounts ||
        mount | grep -qs "$path"
    return $?
}

# Usage:
if is_mounted "/mnt/foo"; then
    # Safe to proceed
fi

When writing Bash scripts that interact with mounted volumes, it's crucial to verify whether a specific mount point exists before performing operations. This prevents errors when dealing with network shares, removable drives, or automated backup systems.

The most straightforward method is using the mountpoint command, which is specifically designed for this purpose:

if mountpoint -q /mnt/foo; then
    echo "Volume is mounted at /mnt/foo"
    # Your operations here
else
    echo "Volume is not mounted"
    # Alternative operations
fi

The -q flag makes the command quiet, only returning an exit status (0 if mounted, 1 if not).

For systems without mountpoint, you can parse /proc/mounts:

if grep -qs '/mnt/foo ' /proc/mounts; then
    echo "Volume is mounted"
else
    echo "Volume not found"
fi

The -s suppresses error messages, and the space after the mount path prevents partial matches.

Another robust alternative is findmnt, which provides more detailed information:

if findmnt -M /mnt/foo >/dev/null; then
    echo "Mount exists"
    # Check specific filesystem type if needed
    if findmnt -M /mnt/foo -o FSTYPE -n | grep -q 'ext4'; then
        echo "It's an ext4 filesystem"
    fi
fi

For network mounts that might be temporarily unavailable, add a readiness check:

check_mount() {
    local mountpoint=$1
    local attempts=5
    local delay=2
    
    while (( attempts-- )); do
        if mountpoint -q "$mountpoint" && [[ -r "$mountpoint" ]]; then
            return 0
        fi
        sleep $delay
    done
    return 1
}

if check_mount "/mnt/nfs_share"; then
    echo "NFS share ready for operations"
else
    echo "Failed to access NFS share" >&2
    exit 1
fi

Sometimes you need to verify not just the mount existence but specific properties:

mount_info() {
    local mp=$1
    if mountpoint -q "$mp"; then
        df -h "$mp" | awk 'NR==2 {print "Device:",$1,"Size:",$2,"Used:",$3,"Avail:",$4}'
        mount | grep " on $mp " | awk '{print "Type:",$5,"Options:",$6}'
        return 0
    fi
    return 1
}

mount_info "/mnt/backup" || echo "Backup volume not mounted"

For scripts dealing with multiple volumes, create a reusable function:

verify_mounts() {
    local -a mounts=("$@")
    local all_ok=true
    
    for mp in "${mounts[@]}"; do
        if ! mountpoint -q "$mp"; then
            echo "ERROR: $mp not mounted" >&2
            all_ok=false
        fi
    done
    
    $all_ok || exit 1
}

# Usage:
required_mounts=("/mnt/data" "/mnt/backup" "/mnt/logs")
verify_mounts "${required_mounts[@]}"

Depending on your specific needs and system environment, choose between mountpoint for simplicity, /proc/mounts for compatibility, or findmnt for detailed information. Always include error handling for cases where the mount exists but isn't accessible.