Shell – How to get mount point of filesystem containing given file

filesystemsmountshell

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 …

Best Answer

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.

Related Question