19

I need a platform-independent and lightweight way of detecting the mount type of my CWD. Specifically, I need to determine if my CWD is in an SSHFS, NFS, CIFS, SMB, or local block store mount.

I need to do this on Mac OSX 10.6, 10.7, and RHEL5 (at minimum).

There's a possibility of using df, but df behaves quite differently on these two platforms. Plus, I'd have to CLI scrape its output with awk, which while workable is... fragile.

This info must be available on some level. Is there a tool that I'm unaware of that could provide this information to me?

17

The filesystem type isn't something you can easily access in an OS-independent way. Filesystem type names are OS-dependent, so there hasn't been any incentive to establish a standard method.

On non-embedded Linux systems, stat -f -c %T . shows the filesystem type of the current directory. OSX's stat command doesn't have anything like this.

Since all you want to know is whether the filesystem type is in a finite list, under Linux, you can limit both the set of paths to print information for and the set of filesystem types. You'll get an error (“df: no file systems processed”) if the filesystem type of the current directory isn't in the list.

if df -t sshfs -t nfs -t cifs . >/dev/null 2>/dev/null; then …

The same syntax may or may not work on OSX; if it doesn't, try df -T sshfs,nfs,cifs ..

1
  • stat reports ext4 as ext2/ext3 $ df -hT . Filesystem Type Size Used Avail Use% Mounted on /dev/mapper/luks-c2be2061-c24c-4d8b-b706-33376dc66ac8 ext4 452G 87G 343G 21% / $ stat -f -c %T . ext2/ext3 use findmnt as suggested by rudimeier or df -TP . | awk '{ print $2}' Feb 12 '21 at 22:47
9

You can use GNU stat on Linux:

stat --file-system --format=%T /tmp/subdir/whatever
tmpfs
2
  • This is great because it works on subdirectories. Thanks! Sep 2 '16 at 18:22
  • -1 This answer would have been better as an edit to Gilles' as it is the same command with the long version of stat arguments. Aug 3 '21 at 7:33
6

On Linux there is findmnt available since util-linux 2.18.

findmnt -n -o FSTYPE -T /home
nfs4
2
  • Doesn't work for /home/subdir, so it wouldn't help the OP.
    – Eloff
    May 18 '16 at 14:36
  • For me it works for subdirs too, using findmount from util-linux 2.23.
    – rudimeier
    Jun 2 '16 at 14:06
3

Haven't tested it, but df -P should behave the same on most platforms.

This works on Linux.

$ mount | grep "^$(df -Pk . | head -n 2 | tail -n 1 | cut -f 1 -d ' ') " | cut -f 5 -d ' '

Or breaking it into reusable functions:

# get_mount <directory>
get_mount() {
    df -Pk "$1" | head -n 2 | tail -n 1 | cut -f 1 -d ' '
}

# get_fs <mountpoint>
get_fs() {
    mount | grep "^$1 " | cut -f 5 -d ' '
}

And calling the function:

get_fs $(get_mount .)

Could be re-written to be slightly faster using sed or awk, but this way's probably easier to read.

If this doesn't work, you could try something similar, but using /etc/mtab rather than the output of mount.

3
  • The P in df -P is for POSIX, so it should behave in the same way. However, getting from the filesystem path to the mount point is more fiddly. Oct 2 '11 at 1:45
  • 1
    Your solution does not seem to be safe if directory is mounted with -o loop. At least on my system df shows the used loop device but mount shows the used image file. I guess that there could be even more cases where df does not show the same device like mount. Note this quotation from mount's man page "The listing mode is maintained for backward compatibility only. For more robust and definable output use findmnt(8), especially in your scripts."
    – rudimeier
    Mar 11 '14 at 13:40
  • The cut thing in the end is quite wobbly since it's not standarized at all. Jun 16 '20 at 12:31

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.