Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

I am looking for a quick way to find the mount point of the file system containing a given FILE. Is there anything simpler or more direct than my solution below?

df -h FILE |tail -1 | awk -F% '{print $NF}' | tr -d ' '

A similar question "Is there a command to see where a disk is mounted?" uses the current disk's device node as input, and not an arbitrary file from the disk ...

share|improve this question
1  
You can do away with the final tr call by using awk -F'% '... –  Joseph R. Sep 11 '13 at 13:56
add comment

3 Answers

up vote 5 down vote accepted

You could do something like

df -P FILE | awk 'NR==2{print $NF}'

or even

df -P FILE | awk 'END{print $NF}'

Since awk splits on whitespace(s) by default, you don't need to specify the -F and you also don't need to trim the whitespace with tr. Finally, by specifying the line number of interest (NR==2) you can also do away with tail.

share|improve this answer
    
the 2nd incantation worked out-of-the-box, while I had to change 2 to 3 in the first. neat –  Stu Sep 11 '13 at 22:55
    
@Gilles, thanks for the edit. One question, the second should work even without -P right? In all cases, the very last field printed by awk should be the disk. –  terdon Sep 11 '13 at 23:34
    
@Stu that's probably because I had not used the -P option that Gilles just added. –  terdon Sep 11 '13 at 23:35
1  
@terdon Yes, indeed, the last field of the last line is the same without -P. Nonetheless I recommend always using -P when you're parsing the output of df, it's easier than checking that this particular use is safe. –  Gilles Sep 11 '13 at 23:37
add comment

On GNU/Linux, if you have GNU stat from coreutils 8.6 or above, you could do:

stat -c %m -- "$file"

Otherwise:

mount_point_of() {
  f=$(readlink -e -- "$1") &&
    until mountpoint -q -- "$f"; do
      f=${f%/*}; f=${f:-/}
    done &&
    printf '%s\n' "$f"
}

Your approach is valid but assumes the mount point doesn't contain space, %, newline or other non-printable characters, you can simplify it slightly with newer versions of GNU df (8.21 or above):

df --output=target FILE | tail -n +2
share|improve this answer
    
My df doesn't recognize the --output option. –  Joseph R. Sep 11 '13 at 16:03
    
@JosephR. is it version 8.21 or above? –  terdon Sep 11 '13 at 16:57
    
@terdon No it's version 8.13. –  Joseph R. Sep 11 '13 at 22:49
1  
@JosephR. Stephane explains in his answer that this is a feature of GNU df >=8.21. –  terdon Sep 11 '13 at 23:35
    
@terdon Sorry must have missed it while skimming. –  Joseph R. Sep 11 '13 at 23:43
show 1 more comment

Since stat returns a "Device" field, I was curious to see how the underlying stat() library call could be used to get this information programmatically in a POSIX compliant way.

This snippet of C code:

#include <stdio.h>
#include <sys/stat.h>
#include <sys/types.h>

int main (int argc, const char *argv[]) {
    struct stat info;
    stat(argv[1], &info);
    printf("min: %d maj: %d\n",
        minor(info.st_dev),
        major(info.st_dev)
    );

    return 0;
}  

Will give the major and minor devices ID's for the device containing the file listed on the command line (argv[1]). Unfortunately, major() and minor() aren't POSIX, although the man page claims they are "present on many other systems " besides GNU/linux.

You can then get a correspondence between the device major/minor number and the device node from, e.g., /proc/diskstats, and map that to mount points from /proc/mounts, aka. /etc/mtab.

So a command-line utility to do this would be pretty simple.

share|improve this answer
    
/proc/diskstats is only for block devices, you'll miss the NFS, proc, fuse... On Linux at least, different mountpoints can have the same maj+min –  Stephane Chazelas Sep 11 '13 at 15:28
    
Did not know that, thx. It also seems that st_dev may not provide a way to distinguish one NFS partition from another. Whoever actually wants to write this will have to take that into account ;) –  TAFKA 'goldilocks' Sep 11 '13 at 15:30
    
+1 for being enough of a geek that you consider writing C code "simpler or more direct" than what the OP was doing :). –  terdon Sep 12 '13 at 1:19
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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