I think you want something like this:
findmnt -T .
When using the option
-T, --target path
if the path is not a mountpoint file or directory,
findmnt
checks path elements in reverse order to get the mountpoint. You can print only certain fields via
-o, --output [list]
.
See
findmnt --help
for the list of available fields.
Alternatively, you could run:
(until findmnt . ; do cd .. ; done)
The problem you're running into is that all paths are relative to something or other, so you just have to walk the tree. Every time.
findmnt
is a member of the util-linux package and has been for a few years now. By now, regardless of your distro, it should already be installed on your Linux machine if you also have the mount
tool.
man mount | grep findmnt -B1 -m1
For more robust and customizable output use
findmnt(8), especially in your scripts.
findmnt
will print out all mounts' info without a mount-point argument, and only that for its argument with one. The -D
is the emulate df
option. Without -D
its output is similar to mount
's - but far more configurable. Try findmnt --help
and see for yourself.
I stick it in a subshell so the current shell's current directory doesn't change.
So:
mkdir -p /tmp/1/2/3/4/5/6 && cd $_
(until findmnt . ; do cd .. ; done && findmnt -D .) && pwd
OUTPUT
TARGET SOURCE FSTYPE OPTIONS
/tmp tmpfs tmpfs rw
SOURCE FSTYPE SIZE USED AVAIL USE% TARGET
tmpfs tmpfs 11.8G 839.7M 11G 7% /tmp
/tmp/1/2/3/4/5/6
If you do not have the -D
option available to you (Not in older versions of util-linux) then you need never fear - it is little more than a convenience switch in any case. Notice the column headings it produces for each call - you can include or exclude those for each invocation with the -o
utput switch. I can get the same output as -D
might provide like:
findmnt /tmp -o SOURCE,FSTYPE,SIZE,USED,AVAIL,USE%,TARGET
OUTPUT
SOURCE FSTYPE SIZE USED AVAIL USE% TARGET
tmpfs tmpfs 11.8G 1.1G 10.6G 10% /tmp
stat
command can be used as well. However, I am not sure if%m
option which gives the mount point is supported in your version of system. I checked in my system and it seemed to not return the mount point.stat "--printf=%m\n" .
gets the mount-point of the file-system that the current directory is in. Thus allowing us to simplify some of the answers. Thanks.