I can do df . to get some of the info on the mount that the current directory is in, and I can get all the info I want from mount. However I get to much info (info about other mounts). I can grep it down, but am wondering if there is a better way.
Is there some command mountinfo such that mountinfo . gives info I want (like df ., but with the info that mount gives.)
I am using Debian Gnu+Linux.
Answers:
Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.
Method 1
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
.
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 -output 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
Method 2
I don’t know of a command, but you could create a function. You can add the below to your .bashrc:
mountinfo () {
mount | grep $(df -P "$1" | tail -n 1 | awk '{print $1}')
}
This executes the mount command and passes the output to grep. grep will look for the output of df -P "$1" | tail -n 1 | awk '{print $1}', and to break it down:
df -P "$1"will rundfon the argument passed to the function,tail -n 1will only output the second line, the one that contains thepartitioninfo.awk '{print $1}'will print the first part of that line, which is the disk/partition number, for example/dev/sda5. That’s whatgrepwill look for in the mount command, and output it.
Source your .bashrc file to apply the changes, or log out and log back in.
Now, if you run mountinfo ., you’ll get the output you want.
Method 3
The Linux/Unix way is to have a toolbox of small utilities that, when combined, give you the results that you’re after.
They tend not to have an utility for every occassion. Instead you have many small useful utilities that are combined together with pipes etc. The advantage of this is that you can write your own utility quite easily if none are available.
For example, to get the info you’re after, you could use:
mount | grep $(df --output=source . | tail -1)
If you want to reuse the above with different directories, create a script:
#!/bin/bash mount | grep $(df --output=source $1 | tail -1)
Save it as mountinfo and make it executable (chmod +x mountinfo). You can then use it as:
mountinfo .
If you want a system that has an utility for everything none of which interoperate with each other, then a certain Mr Gates may be able to help you 😉
Method 4
It can be somewhat messy if the mount points contain blanks, but this should work except in cases where the mount points contain newlines:
#!/bin/sh
mountpoint="$(df -P "$1" | awk '{
if (NR==1)
i=index($0,"Mounted on");
else
print substr($0,i);
}')"
mount|grep " on ${mountpoint} type "
df -P outputs one line for the filesystem; without that option, df may output two lines if the mount point is long. The mount point name starts in the same column as does the “Mounted on” label in the header line.
After we get the mount point, we grep for it in the output of mount.
Method 5
If you using a not completely ancient version of the GNU coreutils df, then echo $(df . --output=target | tail -n 1) works. Likewise with stat -c '%m' ., although I note that https://linux.die.net/man/1/stat lacks documentation of the %m format option.
Doing this portably is tiresome: neither of the above will work with the BSD or Cygwin versions of stat or df. If it weren’t for the possibility of spaces in the paths of mount points, df -P . | tail -1 | awk '{ print $NF}' would be good, but often USB flash drives have spaces in the volume name, which will probably appear in the mount path and break this. For portability, Perl is installable practically everywhere and CPAN has the module Sys::Filesystem::MountPoint which provides the desired functionality.
All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0