brewedbits.dev

Personal blog with guides and news on selfhosting, Linux and AI, from the Netherlands

nextDash nextdash --aboutYour bookmarks. Your terminal. checks your links knows your Docker host one container, no cloudLive demo →nextdash.cc ↗

Tip: mountpoint -q tells a script whether a folder is really mounted

Linux
~/tips/tip-linux-mountpoint-q-script-check

if ! mountpoint -q /mnt/backup; then

Returns success only when a folder is a mount point, so scripts can check first.

A backup script that writes to /mnt/backup while the drive is not mounted will happily fill the root disk instead. A one-line check avoids that.

mountpoint exits with 0 when the given directory is a mount point and with 32 when it is not. Add -q to keep it quiet and use only the exit code:

if ! mountpoint -q /mnt/backup; then
  echo "backup drive not mounted" >&2
  exit 1
fi
rsync -a /mnt/user/appdata/<app>/ /mnt/backup/appdata/

Without -q it prints a plain sentence. For example, mountpoint / answers / is a mountpoint. Exit code 1 means a bad invocation, a permission problem or a system error. A path that does not exist gives No such file or directory and exit code 1 in my test, so a typo in the path ends up as “not mounted” in the script above. Testing with ! treats all of these as failures, which is the safe way round for this use.

The man page has two related options. -d shows the major and minor numbers of the device mounted on the directory, and -x shows them for a block device you name.

It only tells you that something is mounted there. It does not say whether it is the device you expected. If the path is a bind mount, or a network share that has gone stale, the answer may not match what you hoped. When you want to check the source, findmnt shows more detail about what is mounted where.

Further reading: mountpoint(1) manual page

More Linux tips a2query -m rewrite renice -n 10 -p 4242 cat config.yml | xsel -ib xvfb-run -a your-program --some-flag dpkg --get-selections > packages.txt All Linux tips (261) All topics

Link