Let's say that /var/log is /dev/sda2, and /var/spool is part of /var which is part of / which is /dev/sda1.

Now I'd like to issue a command like

$ sudo get_mountpoint_of /var/spool

and get the correct answer:

/var/spool is part of / and is mounted on /dev/sda1

on a linux system you can do:

findmnt -nvoSOURCE -T /*some/path

...to print only the source-device for whatever file is referred to by /*some/path.

options breakdown:

  • -n

    • omits column headings
  • -oSOURCE

    • selects only the SOURCE device output column, though there are many others.

    • append ,TARGET to list the mount-point column as well.

  • -v

    • omits the [...] portion of any possible /dev/device[/bindmnt] result
  • -T

    • instructs findmnt to work backwards through its --target argument until it finds a valid mount-point before reporting.

for more information:

man findmnt

You do not need sudo to do this. You could write a script which did "df" for the starting point, and read the mount point from the output. If the mount point is not "/", then your script would recur, asking where that was mounted.

For instance, on the machine at hand, I have

$ df /var/log
Filesystem                                             1K-blocks     Used Available Use% Mounted on
/dev/disk/by-uuid/8a1efcd1-0d32-4674-aa7a-c24c2cd924fd  19751804 12437492   6310948  67% /

For generality, your script would have to recognize that (for this example), the disk information may be managed by LVM or udev, and getting details for that likely requires sudo).

The case posed by OP is simpler of course. For the same machine, I have a filesystem on a disk device:

$ df /users
Filesystem     1K-blocks    Used Available Use% Mounted on
/dev/sdb1        8255928 5548304   2288248  71% /users

and again, the script would only have to look at the output of "df" to see whether to finish, or recur, asking where that was mounted.


For mountpoint info

df -P file/goes/here | tail -1 | cut -d' ' -f 1 

For "part of" info

df /mountpointFromTheLastCommand | tail -1 | awk '{print $6}'

Example :

[root@xxx ~]# df -P /root/download/ | tail -1 | cut -d' ' -f 1
[root@xxx ~]# df /dev/sda2 | tail -1 | awk '{print $6}'

And with these informations, you can say /root/download is part of / and is mounted on /dev/sda2 =]

This is the idea, you can optimize it and make it in one line with your output you want.

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.