Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Give the mount point of a path

The following, very non-robust shell code will give the mount point of $path:

 (for i in $(df|cut -c 63-99); do case $path in $i*) echo $i;; esac; done) | tail -n 1

Is there a better way to do this in shell?

Postscript

This script is really awful, but has the redeeming quality that it Works On My Systems. Note that several mount points may be prefixes of $path.

Examples On a Linux system:

cas@txtproof:~$ path=/sys/block/hda1
cas@txtproof:~$ for i in $(df -a|cut -c 57-99); do case $path in $i*) echo $i;; esac; done| tail -1
/sys

On a Mac OSX system

cas local$ path=/dev/fd/0
cas local$ for i in $(df -a|cut -c 63-99); do case $path in $i*) echo $i;; esac; done| tail -1
/dev

Note the need to vary cut's parameters, because of the way df's output differs; using awk solves this, but even awk is non-portable, given the range of result formatting various implementations of df return.

Answer It looks like munging tabular output is the only way within the shell, but

df -P "$path"  | tail -1 | awk '{ print $NF}'

based on ghostdog74's answer, is a big improvement on what I had. Note two new issues: firstly, df $path insists that $path names an existing file, the script I had above doesn't care; secondly, there are no worries about dereferencing symlinks. This doesn't work if you have mount points with spaces in them, which occurs if one has removable media with spaces in their volume names.

It's not difficult to write Python code to do the job properly.

like image 450
Charles Stewart Avatar asked Jan 30 '10 10:01

Charles Stewart


People also ask

What is a mount path?

Mount Path. The path to which the data is written to and read from. Free Space. Not available for cloud storage. Size on Disk.

How do I find the mount point of a file?

Use the lsfs command to display information about mount points, permissions, file system size and so on.

How do you create a mount point?

In Disk Manager, right-click the partition or volume that has the folder in which you want to mount the drive. Click Change Drive Letter and Paths and then click Add. Click Mount in the following empty NTFS folder. Type the path to an empty folder on an NTFS volume, or click Browse to locate it.


2 Answers

df takes the path as parameter, so something like this should be fairly robust;

df "$path" | tail -1 | awk '{ print $6 }'
like image 96
JesperE Avatar answered Sep 27 '22 22:09

JesperE


In theory stat will tell you the device the file is on, and there should be some way of mapping the device to a mount point.

For example, on linux, this should work:

stat -c '%m' $path
like image 37
Douglas Leeder Avatar answered Sep 27 '22 21:09

Douglas Leeder