LinuxCommandLibrary

mountpoint

Check if a directory is a mountpoint

TLDR

Check if a directory is a mountpoint

$ mountpoint [path/to/directory]
copy

Check if a directory is a mountpoint without showing any output
$ mountpoint [[-q|--quiet]] [path/to/directory]
copy

Show major/minor numbers of a mountpoint's filesystem
$ mountpoint [[-d|--fs-devno]] [path/to/directory]
copy

SYNOPSIS

mountpoint [options] directory|
mountpoint [options] --devno major:minor|
mountpoint [options] --fsname device

PARAMETERS

-d, --debug
    Prints debug information to standard error.

-q, --quiet
    Suppresses all messages, returning only an exit status. This is commonly used in scripts.

-k, --kernel
    Checks the kernel mount table (/proc/mounts or /etc/mtab) in addition to stat(2) information. This is the default behavior.

-n, --no-mtab
    Checks stat(2) information only, specifically comparing the device ID (st_dev) of the target and its parent. It skips checking the kernel mount table.

-x, --devno
    Prints the device number of the given mountpoint in `major:minor` format.

--devno major:minor
    Checks if the filesystem identified by the specified device number is mounted.

--fsname device
    Checks if the filesystem identified by the specified device name (e.g., /dev/sda1) is mounted.

-V, --version
    Displays version information and exits.

--help
    Displays a help message and exits.

DESCRIPTION

The `mountpoint` command is a simple yet powerful utility used to determine if a specified directory or file is the root of a mounted filesystem. It returns an exit status of 0 if the path is a mountpoint, and 1 if it is not, making it ideal for use in shell scripts where programmatic checks are needed.

By default, `mountpoint` checks both the stat(2) information (comparing the device ID, st_dev, of the given path with its parent directory) and the kernel's mount table (typically /proc/mounts or /etc/mtab). This dual check ensures accuracy, identifying both standard mounts and bind mounts. It can also be used to check if a specific block device or filesystem name is currently mounted. This command is invaluable for system administrators and scripters to ensure that operations like unmounting or cleaning up only proceed when a path is confirmed to be a filesystem boundary.

CAVEATS

  • Symbolic Links: When provided with a path that is a symbolic link, `mountpoint` checks the resolved path, not the symlink itself.
  • Root Filesystem: The root directory (/) is always considered a mountpoint for the root filesystem, so `mountpoint /` will typically return success (0).
  • Bind Mounts: `mountpoint` correctly identifies bind mounts as mountpoints.
  • Files vs. Directories: While primarily used for directories, `mountpoint` can theoretically check a file. However, for a file to be a mountpoint, a filesystem must be mounted directly onto that file, which is an uncommon setup (e.g., a loop device mounted directly on a file).

EXIT STATUS

The command's exit status is crucial for scripting:

  • 0: The specified path is a mountpoint.
  • 1: The specified path is not a mountpoint.
  • >1: An error occurred (e.g., invalid arguments, path not found).

HISTORY

The `mountpoint` command is a part of the util-linux package, a collection of essential system utilities for Linux. Its core functionality, which relies on inspecting device IDs and the kernel mount table, has remained stable and largely unchanged over time. It has been a reliable tool for scripting and system administration for many years, reflecting its fundamental utility in managing and querying filesystem states.

SEE ALSO

mount(8), umount(8), findmnt(8), stat(1), df(1)

Copied to clipboard