Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Check if Directory is Mounted in Bash on Linux
Checking if a directory is mounted is a common system administration task in Linux. There are several reliable methods to determine whether a specific directory serves as a mount point, each with different advantages and use cases.
Using the mount Command
The mount command is the most traditional method to check mounted filesystems. To check if a specific directory is mounted, combine it with grep ?
mount | grep "/mnt/data"
If the directory is mounted, this returns information about the mount point, filesystem type, and source device. If not mounted, no output is displayed.
You can also use mount options for different views ?
mount -l # List all mounted filesystems mount -t ext4 # Show only ext4 filesystems mount -v # Verbose output
Using the findmnt Command
The findmnt command provides more detailed and structured output for mount point information ?
findmnt -T /mnt/data # Check specific mount point findmnt --mountpoint /mnt/data # Alternative syntax
This displays detailed information including filesystem type, source device, and mount options in a tree format. Additional useful options ?
findmnt -l # List format instead of tree findmnt -t ext4 -o TARGET # Show only ext4 mount points
Using the mountpoint Command
The mountpoint command is specifically designed to test if a directory is a mount point ?
mountpoint /mnt/data # Returns 0 if mounted, 1 if not mountpoint -q /mnt/data # Quiet mode (no output)
This command is particularly useful in shell scripts because it provides a clear exit status. However, it may not be available on all Linux distributions.
Reading /proc/mounts
The /proc/mounts file contains real-time information about all mounted filesystems ?
cat /proc/mounts # Show all mounts grep "/mnt/data" /proc/mounts # Check specific mount awk '$2=="/mnt/data"' /proc/mounts # Extract matching mount
This method is always available and provides direct access to kernel-maintained mount information.
Bash Scripting Examples
Here are practical examples for checking mount status in bash scripts ?
#!/bin/bash
# Method 1: Using mountpoint
if mountpoint -q /mnt/data; then
echo "/mnt/data is mounted"
else
echo "/mnt/data is not mounted"
fi
# Method 2: Using findmnt
if findmnt /mnt/data > /dev/null; then
echo "/mnt/data is mounted"
else
echo "/mnt/data is not mounted"
fi
# Method 3: Using grep with mount
if mount | grep -q "/mnt/data"; then
echo "/mnt/data is mounted"
else
echo "/mnt/data is not mounted"
fi
Comparison of Methods
| Method | Advantages | Disadvantages | Best Use Case |
|---|---|---|---|
| mount + grep | Universal availability, simple | Text parsing required | Quick manual checks |
| findmnt | Structured output, detailed info | Not on older systems | Detailed analysis |
| mountpoint | Clear exit status, script-friendly | Limited availability | Shell scripting |
| /proc/mounts | Always available, direct kernel data | Requires text parsing | System programming |
Conclusion
Multiple methods exist to check if a directory is mounted in Linux, each suited for different scenarios. For interactive use, findmnt provides the most informative output, while mountpoint is ideal for scripting. The traditional mount command remains universally available across all Linux systems.
