Check if Directory is Mounted in Bash on Linux


You can use the "mount" command to check if a directory is mounted on a Linux system.

For example, to check if the directory "/mnt/data" is mounted, you can run −

mount | grep "/mnt/data"

If the directory is mounted, the command will return information about the mount point, including the file system type and the device it is mounted on. If the directory is not mounted, the command will return nothing.

You can also check if a directory is a mount point using the findmnt command.

findmnt -T /mnt/data

This command will show all the mountpoints for the given directory path.

Using the mount Command

Yes, the "mount" command can be used to check if a directory is mounted on a Linux system.

The basic syntax of the "mount" command is −

mount [-lhV]

The -l option is used to display all the file systems that are currently mounted on the system.

mount -l

The -t option is used to display all the file systems of a specific type that are currently mounted on the system.

mount -t ext4

The -v option is used to display the information in a verbose format, which can be useful for troubleshooting.

mount -v

You can also use the findmnt command to display information about a specific mount point.

findmnt /mnt/data

This will show detailed information about the mount point at /mnt/data, including the file system type, the device it is mounted on, and the options used when it was mounted.

Using the mountpoint Command

Yes, the "mountpoint" command is another way to check if a directory is a mount point on a Linux system.

The basic syntax of the "mountpoint" command is −

mountpoint [-qdnx] path

The -q option is used to display the exit status, 0 if the path is a mount point and 1 if it is not.

mountpoint -q /mnt/data

The -d option is used to display the information in a verbose format, which can be useful for troubleshooting.

mountpoint -d /mnt/data

The -n option is used to check if path is a mountpoint but do not print any messages.

mountpoint -n /mnt/data

The -x option is used to check if path is not a mountpoint but do not print any messages.

mountpoint -x /mnt/data

It's worth noting that the mountpoint command is not present on all distributions and it might not be available in your system.

Using the findmnt Command

Yes, the "findmnt" command is another way to check if a directory is a mount point on a Linux system.

The basic syntax of the "findmnt" command is −

findmnt [options] [source]

To check a specific mount point, you can use the -T option followed by the path to the mount point.

findmnt -T /mnt/data

This command will display detailed information about the mount point at /mnt/data, including the file system type, the device it is mounted on, and the options used when it was mounted.

If you want to list all the mounted file systems, you can use the -l option.

findmnt -l

You can also filter the output by file system type, device, options, etc. using the findmnt command, this can be done by combining different options to the command.

findmnt -t ext4 -o TARGET

This command will show all the ext4 filesystems and their mount points.

You can also use findmnt with the --mountpoint option to check if a directory is a mount point.

findmnt --mountpoint /mnt/data

This command will display detailed information about the mount point at /mnt/data, if it exists. If not, it will return nothing.

Reading /proc/mounts

Another way to check if a directory is a mount point on a Linux system is by reading the file /proc/mounts.

This file is a virtual file that contains information about all the file systems currently mounted on the system. It is a plain text file with each line representing a file system and its properties, such as the device name, the mount point, and the file system type.

You can use the "cat" command to display the contents of this file −

cat /proc/mounts

You can also use "grep" command to search for a specific mount point in this file −

grep "/mnt/data" /proc/mounts

This will display a line with the information about the file system mounted on /mnt/data, if it exists. If not, it will return nothing.

You can also use "awk" command to extract specific information from the file, for example −

awk '{print $2}' /proc/mounts

This command will print the second column of the file which is the mount point.

Keep in mind that the contents of this file are generated at runtime by the kernel and it may not be accurate if the system is undergoing changes, like mounting or unmounting a filesystem.

Conclusion

In conclusion, there are several ways to check if a directory is a mount point on a Linux system. The most common ways are using the mount, findmnt, or reading the /proc/mounts file.

The mount command can be used to display all the file systems that are currently mounted on the system, and it also accepts options such as -t to display all the file systems of a specific type, -v for verbose output, and -l for a list of all the mounted file systems.

The findmnt command can be used to display information about a specific mount point or list all the mounted file systems. It also accepts options such as -T to check a specific mount point, -l to list all the mounted file systems and -t to filter the output by file system type.

The /proc/mounts file is a virtual file that contains information about all the file systems currently mounted on the system. You can use the "cat" or "grep" command to display or search for a specific mount point in this file.

Updated on: 25-Jan-2023

24K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements