In *nix, how to determine which filesystem a particular file is on?

11,894

Solution 1

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.

Solution 2

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

Solution 3

For just a specific file it's as easy as

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

Solution 4

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

Solution 5

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
Share:
11,894

Related videos on Youtube

smokris
Author by

smokris

Updated on September 17, 2022

Comments

  • smokris
    smokris almost 2 years

    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.

  • Steve Townsend
    Steve Townsend over 14 years
    Avoid this gotcha by using 'df -P' to get output in POSIX format and without line-breaking.
  • Peter John Acklam
    Peter John Acklam over 12 years
    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.
  • daisy
    daisy almost 12 years
    So, How to find the device name base on that ?
  • Wiesław Herr
    Wiesław Herr over 11 years
    You need to go though all the device files in /dev/ and look up one with the same minor number as stat reported.
  • Dan Moulding
    Dan Moulding over 9 years
    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.
  • Aleksi Torhamo
    Aleksi Torhamo over 9 years
    @CharlesStewart: Sadly, there's no such function in Python to my knowledge. os.path.splitunc() only works for UNC paths and is only available on Windows.
  • Ciprian Amaritei
    Ciprian Amaritei about 9 years
    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.
  • roaima
    roaima about 9 years
    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.
  • 0xC0000022L
    0xC0000022L about 9 years
    Doesn't work with btrfs!
  • 0xC0000022L
    0xC0000022L about 9 years
    @roaima: careful with btrfs subvolumes. It's bound to fail there.
  • roaima
    roaima about 9 years
    @0xC0000022L thank you for heads-up on btrfs. It's in my corner of things to try at home (lab)
  • Tom Hale
    Tom Hale over 6 years
    @0xC0000022L I tried it and it worked fine on a mounted subvol. What issue did you expect / see?
  • 0xC0000022L
    0xC0000022L over 6 years
    @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 :)
  • Hasanuzzaman Sattar
    Hasanuzzaman Sattar about 6 years
    Example df /path-to-the-directory will give you the containing partition of that directory