14

In a generic, modern unix environment (say, GNU/Linux, GNU/Solaris, or Mac OS X), is there a good way to determine which mountpoint and filesystem-type a particular absolute file path is on?

I suppose I could execute the mount command and manually parse the output of that and string-compare it with my file path, but before I do that I'm wondering if there's a more elegant way.

I'm developing a BASH script that makes use of extended attributes, and want to make it Do The Right Thing (to the small extent that it is possible) for a variety of filesystems and host environments.

smokris
  • 685
  • 3
  • 13
  • 27

7 Answers7

21

The command df(1) takes one or more arguments and will return the mountpoint and device on which that file or directory exists, as well as usage information. You can then use the path or device to look up the filesystem type in the output of mount -v or similar.

Unfortunately, the output format of both df and mount are system-dependent; there is no apparent standard, at least as I can see between Solaris, NetBSD and Mac OS X.

Morven
  • 1,031
  • 1
  • 7
  • 15
  • 1
    `df -P` should produce standardized output on any POSIX compliant system. Some goofier systems might require a magic environment variable, such as POSIXLY_CORRECT, to be set as well. – Dan Moulding Nov 04 '14 at 18:13
  • Example `df /path-to-the-directory` will give you the containing partition of that directory – Hasanuzzaman Sattar Jun 23 '18 at 06:15
8

You could use stat. The command stat --printf '%d' filename.txt will return the device number as hex/decimal.

Zoredache
  • 128,755
  • 40
  • 271
  • 413
  • So, How to find the device name base on that ? – daisy Sep 05 '12 at 09:51
  • You need to go though all the device files in /dev/ and look up one with the same minor number as stat reported. – Wiesław Herr Feb 22 '13 at 12:47
  • 1
    `stat --printf "%d"` does tell you the minor number of a device, but there's more work to be done to get the device name and its mounted file system. – Craig McQueen May 04 '15 at 06:24
  • 2
    Maybe it's a recent addition but `stat --format '%m' $file` will give you the mount point, and `stat --file-system --format '%T' $mount` will provide the file system type name. – roaima May 19 '15 at 21:11
  • @roaima: careful with btrfs subvolumes. It's bound to fail there. – 0xC0000022L Jun 01 '15 at 11:41
  • @0xC0000022L thank you for heads-up on btrfs. It's in my corner of things to try at home (lab) – roaima Jun 01 '15 at 12:50
  • @0xC0000022L I tried it and it worked fine on a mounted subvol. What issue did you expect / see? – Tom Hale Oct 31 '17 at 03:49
  • 1
    @TomHale: don't remember, to be honest. But I remember it didn't work. Admittedly I should have stated the distro, kernel version etc. But you stating it works, may also mean it got fixed meanwhile. If the net result is that it works, great :) – 0xC0000022L Nov 01 '17 at 12:19
5

For just a specific file it's as easy as

df -T "${FILE}" | awk '{print $2}' | tail -n1
2

Hm. For the mount point, you can go up the hierarchy until the st_dev changes (then you have just crossed over a mount boundary); there's GNU stat for bash scripts; however, I don't know how you can guess the filesystem type without parsing /proc/mounts or by trial and error (i.e. handle failures after setting extended attributes)

ΤΖΩΤΖΙΟΥ
  • 1,038
  • 1
  • 10
  • 18
2

There seems to be a catch with df and btrfs on Linux. When you ask df to locate the mount point for a mounted btrfs volume, it will do the right thing. In this case, joe is a sub-directory of /m/whale/backup.

# df /srv/backup/joe
Filesystem      1K-blocks      Used  Available Use% Mounted on
/dev/md126     2930135488 307676684 2619663252  11% /m/whale/backup

But if the directory being referenced is a sub-volume, it won't tell you the mount point anymore.

# df /srv/backup/joe/code
Filesystem      1K-blocks      Used  Available Use% Mounted on
-              2930135488 307676684 2619663252  11% /a/whale/backup/joe/code

The /a/whale/backup is the only mount point according to the kernel.

# mount | grep whale
/dev/md126 on /a/whale/backup type btrfs (rw,relatime,space_cache)

FWIW, stat does the same thing:

# stat --printf '%m\n' /srv/backup/joe/code
/a/whale/backup/joe/code
2

One gotcha with using df is that if the device name in the output is long it's line will wrap so you can't just grab the last line. Instead strip off the first line and then grab the new first line and then print the first field:

#!/usr/bin/env bash

path=$1
curdir=$(pwd)
cd $path
df . | tail -n +2 | head -1 | awk '{print $1}'
cd $curdir
mage2k
  • 489
  • 3
  • 3
  • 3
    Avoid this gotcha by using 'df -P' to get output in POSIX format and without line-breaking. – MikeyB Dec 22 '09 at 05:01
1

From https://stackoverflow.com/questions/2167558/give-the-mount-point-of-a-path:

 df -P $path  | tail -1 | awk '{ print $NF}'

works everywhere I have tested, for both *BSDs and sysVs, and for wacky automounted directories. I'd be delighted to hear of a case where it fails.

Charles Stewart
  • 650
  • 6
  • 19
  • 1
    The suggested code df -P $path | tail -1 | awk '{ print $NF}' fails on all versions of Solaris I have tried (2.5.1, 8, 9, and 10) because Solaris’ “df” does not support the “-P” option. – Peter John Acklam Nov 16 '11 at 09:23
  • @Peter: I am less delighted than I thought I would be. But it is interesting to know that the problem is nontrivial. I think the right things is to write a command in a scripting language whose library has solved the problem properly, e.g., Python has the os.path.splitunc() function that gives the mount point and which I assume works on Solaris. – Charles Stewart Nov 17 '11 at 11:26
  • @CharlesStewart: Sadly, there's no such function in Python to my knowledge. `os.path.splitunc()` only works for [UNC paths](http://en.wikipedia.org/wiki/Path_%28computing%29#Uniform_Naming_Convention) and is [only available on Windows](https://docs.python.org/2/library/os.path.html#os.path.splitunc). – Aleksi Torhamo Dec 12 '14 at 01:07