2

The search results that I've turned up have either been about determining whether a filesystem is mounted at a particular path or whether a particular path is being used as a mount point. I am looking for a way to determine whther or not a given device or file (i.e., a filesystem image) is mounted at all. This solution should be able to handle spaces in the path to device/file.

These, which I have seen in my search results, are NOT solutions:

  • mountpoint -q "${PATH}" only tests if ${PATH} is a mount point.
  • Applying grep -q "${PATH}" to some output may not work because ${PATH} may have spaces in it and the output of commands like mount has spaces in it.

Additionally, it should return WHERE a file/device is mounted to.

  • The difficult part it is handling a mount point with space. So I would put such important point also on the title of thew question. – Giacomo Catenazzi Oct 19 '16 at 15:04
  • Did you try to grep through /proc/mounts ? It should have all informations you need. – Kalavan Oct 19 '16 at 15:16
  • @Kalavan If the source is something /home/user/fs, but another file/device is mounted at /home/user/fs_mnt, then grep is pretty useless. – Melab Oct 19 '16 at 15:23
  • @Melab That's why you grep for the device name, not the mount name. /etc/mtab (for RH and similar systems) stores the mount information for all currently mounted filesystems. So you grep for the device name in /etc/mtab. See the answer provided by a0f3dd13. – Xalorous Oct 19 '16 at 15:32
  • @Xalorous Except if /home/user/fs is a device name… – Melab Oct 19 '16 at 16:42
2

You can consult the file /proc/self/mounts (or /proc/mounts), in this file spaces are encoded using \040 (octal value):

Example, /dev/sda1 is mount in /mnt/mount sda1

grep '/dev/sda1' /proc/mounts | cut -d ' ' -f 2

Return: /mnt/mount\040sda1

grep "$(echo '/mnt/mount sda1' | sed 's/ /\\\\040/g')" /proc/mounts | cut -d ' ' -f 1

Return: /dev/sda1

0

/etc/mtab, /proc/mounts and so on do not display the source file path.

mount commands does and formats its output.

So you could try to:

mount | egrep "${file_path=} on /"

export file_path="/home/kalavan/Downloads/debian 6.0.10-amd64-netinst.iso"

mount | egrep "${file_path=} on /"
/home/kalavan/Downloads/debian 6.0.10-amd64-netinst.iso on /mnt/pc75 type iso9660 (ro,relatime)

In opposite to

mount | grep debian               
/home/kalavan/Downloads/debian 6.0.10-amd64-netinst.iso on /mnt/pc75 type iso9660 (ro,relatime)
/home/kalavan/Downloads/debian 6.0.10-amd64-netinst.iso_somethingelse on /mnt/projekte type iso9660 (ro,relatime)

The risk of mistake is much smaller then. One could also try to match rest of line pattern wit a regexp.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.