Check if Directory is Mounted in Bash on Linux (2024)

Check if Directory is Mounted in Bash on Linux (1)

  • Data Structure
  • Networking
  • RDBMS
  • Operating System
  • Java
  • MS Excel
  • iOS
  • HTML
  • CSS
  • Android
  • Python
  • C Programming
  • C++
  • C#
  • MongoDB
  • MySQL
  • Javascript
  • PHP
  • Physics
  • Chemistry
  • Biology
  • Mathematics
  • English
  • Economics
  • Psychology
  • Social Studies
  • Fashion Studies
  • Legal Studies
  • Selected Reading
  • UPSC IAS Exams Notes
  • Developer's Best Practices
  • Questions and Answers
  • Effective Resume Writing
  • HR Interview Questions
  • Computer Glossary
  • Who is Who

LinuxOpen SourceOperating System

'; var adpushup = adpushup || {}; adpushup.que = adpushup.que || []; adpushup.que.push(function() { adpushup.triggerAd(ad_id); });

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.

Satish Kumar

e

Updated on: 25-Jan-2023

25K+ Views

Related Articles

  • How To Check If File or Directory Exists in Bash?
  • Escaping Characters in Bash on Linux
  • String Manipulation in Bash on Linux
  • Introduction to Bash Globbing on Linux
  • Is there a goto statement available in bash on Linux?
  • Implement a Counter in Bash Script on Linux
  • Parse Command Line Arguments in Bash on Linux
  • Here Document And Here String in Bash on Linux
  • The Meaning of IFS in Bash Scripting on Linux
  • Preserve Bash History in Multiple Terminal Windows on Linux
  • Check if Hard Drive is SSD or HDD on Linux
  • Bash Functions in Linux
  • Negate an if Condition in a Bash Script in Linux
  • Check if a directory is not empty in Java
  • Bash program to check if the Number is a Palindrome?
Kickstart Your Career

Get certified by completing the course

Get Started

Check if Directory is Mounted in Bash on Linux (31)

Advertisem*nts

';adpushup.triggerAd(ad_id); });

Check if Directory is Mounted in Bash on Linux (2024)

FAQs

Check if Directory is Mounted in Bash on Linux? ›

Using the mount Command

How do you check which folder is mounted in Linux? ›

If you want to check if a directory is a mount point, use findmnt with the --mountpoint option. The findmnt --mountpoint /mnt/data command shows detailed information on the mount point at /mnt/data if it exists. If not, it will return nothing.

How do I check if a directory exists in Linux Bash? ›

Check if a Directory Exists in Bash

echo $? echo $? The command output is 0 , which means that the directory exists. The test command works the same as it does for files, so using brackets instead of the test command works here too.

How do I check if a directory has files in Bash? ›

Checking the File Type with a Bash Script

The -e flag is to check whether the files or the directories exist or not. The -f flag is to check whether the ordinary files (not directories) exist or not. Finally, the -d flag is to check whether this is a directory or not.

How to check for mounted drives in Linux? ›

To find out what drives are mounted you can check /etc/mtab , which is a list of all devices mounted on the system. It can sometimes have various tmpfs and other things you aren't looking for mounted too, so I recommend cat /etc/mtab | grep /dev/sd to get only physical devices. Listing from /proc using cat command.

How do you check if a directory is mounted in bash? ›

One way we can determine if a directory is mounted is by running the mount command and filtering the output. The above line will exit with 0 (success) if /mnt/backup is a mount point. Otherwise, it'll return -1 (error).

How do I know if a CD is mounted in Linux? ›

Usually on Linux, when an optical disc is mounted, the eject button is disabled. To determine whether anything is mounted in the optical drive, you can check the contents of /etc/mtab and look for either the mount point (e. g. /mnt/cdrom ) or the device for the optical drive (e. g. /dev/cdrom ).

What directory am I in Bash? ›

To know your current directory, you can use the pwd command which stands for Print Working Directory. The name of the current working directory is the last directory in the absolute path. For example, in the above example, dir2 is the current working directory.

How to check if a directory exists in command line? ›

If you're working from a shell the ls command or stat command are the most common commands to check if a file or directory exists.

How to check if a subdirectory exists in Bash? ›

What command can be used to check if a directory exists or not within a Bash shell script? To check if a directory exists in a shell script, you can use the following: if [ -d "$DIRECTORY" ]; then. # Control will enter here if $DIRECTORY exists.

How do I find a directory in Linux Bash? ›

How to Find a Directory in Linux. Searching for a directory is possible by passing the d to the -type parameter in the find command. In the above screenshot, we're finding a directory named zip from our current directory. Similarly the -type option accepts other parameter options to simplify our finding process.

How to check directory or file in Linux? ›

Use the ls command to display the contents of a directory. The ls command writes to standard output the contents of each specified Directory or the name of each specified File, along with any other information you ask for with the flags.

How do I open a directory in Bash? ›

Basic Bash Commands

cd path-to-directory : The command followed by a path allows you to change into a specified directory (such as a directory named documents ). cd .. (two dots). The .. means “the parent directory” of your current directory, so you can use cd .. to go back (or up) one directory.

How do I see mounted directories in Linux? ›

Using the findmnt Command

Yes, the "findmnt" command is another way to check if a directory is a mount point on a Linux system. To check a specific mount point, you can use the -T option followed by the path to the mount point.

How to check mount and unmount in Linux? ›

Mounting and unmounting media using Linux
  1. Type cd and then press Enter.
  2. Type one of the following commands: If the medium to be unmounted is a CD, type umount /mnt/cdrom. and then press Enter. If the medium to be unmounted is a diskette, type umount /mnt/floppy. and then press Enter.

What is the mount path in Linux? ›

Linux has two default mount point locations for removable media: /dev (device) is where all physical devices are first mounted. From there, an additional link to the removable media is made in the /mnt (mount) directory.

How to check mount space in Linux? ›

The df command stands for "disk-free," and shows available and used disk space on the Linux system. df -T shows the disk usage along with each block's filesystem type (e.g., xfs, ext2, ext3, btrfs, etc.) You can get this information in a graphical view using the Disks (gnome-disk-utility) in the GNOME desktop.

How do I know if a directory is NFS mounted in Linux? ›

Checking NFS mounts in Linux is easy to do with just a few simple commands.
  1. Use the mount Command. The most straightforward way to check NFS mounts on Linux is by using the mount command. ...
  2. Use the df Command. ...
  3. Check Mounts in /etc/mtab. ...
  4. Identify Mount Issues. ...
  5. Automate with a Monitoring Script.
Jan 19, 2024

How to check mounted filesystem type in Linux? ›

How to Determine a Mounted File System's Type
  1. Use the devnm command to display the raw device name of the mounted file system. $ devnm /filesystem. /filesystem. ...
  2. Become superuser.
  3. Use the fstyp command to display the file system type. # fstyp dev/rdsk/ device-name. /dev/rdsk/ device-name.

Top Articles
Latest Posts
Article information

Author: Eusebia Nader

Last Updated:

Views: 6311

Rating: 5 / 5 (60 voted)

Reviews: 83% of readers found this page helpful

Author information

Name: Eusebia Nader

Birthday: 1994-11-11

Address: Apt. 721 977 Ebert Meadows, Jereville, GA 73618-6603

Phone: +2316203969400

Job: International Farming Consultant

Hobby: Reading, Photography, Shooting, Singing, Magic, Kayaking, Mushroom hunting

Introduction: My name is Eusebia Nader, I am a encouraging, brainy, lively, nice, famous, healthy, clever person who loves writing and wants to share my knowledge and understanding with you.