What is the best way (reliable, portable, etc.) to check if a given folder is on a mounted remote (nfs) filesystem within a shell script?

I am looking for a command that would look like:

chk-remote-mountpoint /my/path/to/folder 

10 Answers 10


As Stephane says "there is no universal Unix answer to that".

The best solution I have found to my question:

df -P -T /my/path/to/folder | tail -n +2 | awk '{print $2}'

will return the filesystem type, for example: nfs or ext3.

The -T option is not standard, so it may not work on other Unix/Linux systems...

According to Gilles' comment below: "This works on any non-embedded Linux, but not on BusyBox, *BSD, etc."

  • 1
    This works on any non-embedded Linux, but not on BusyBox, *BSD, etc. It's up to you to decide whether that's good enough. – Gilles Apr 12 '13 at 22:18
  • Viewing /etc/mtab or results of mount instead of using df could do in some more cases. – TNW Apr 16 '13 at 23:50
  • 1
    @TNW or even /proc/mounts – laebshade Apr 17 '13 at 3:55
  • 1
    Apparently, neither mount, nor /etc/mtab, not even /proc/mounts are standard so there is no guarantee on what may be found there... – Totor Apr 18 '13 at 22:44
  • 1
    This not work on OSX 10.8 as -T have a different meaning here – DavAlPi Jun 23 '13 at 16:25

You could use GNU stat.

%m to find out the mountpoint.

$ stat --format=%m /usr/src/linux

%T (in file-system mode) to find out the name of the file system.

$ stat --file-system --format=%T /usr/src/linux

Thus you know that /usr/src/linux, on my system, is stored in a filesystem that is mounted on /usr/src and has the filesystem type reiserfs.

Also refer to man stat for further reference. It's a very versatile command, useful almost always when you need info about files and don't want to fall back to grep | awkwardness.

  • My version of stat does not have the %m option for --format (using Debian Squeeze, coreutils 8.5). So, I guess this solution is not very reliable. – Totor Apr 12 '13 at 19:38
  • I didn't believe it, but diff between coreutils 8.5 and 8.13 (debian wheezy) says you're right, though. Consider me very surprised :) squeeze is just too old :( err, too stable – frostschutz Apr 12 '13 at 19:47
  • 1
    from the coreutils changelog: 2010-08-27 <Aaron Burgemeister> stat: add %m to output the mount point for a file. That's almost 3 years ago. Amazing that Debian still does not have this. – frostschutz Apr 12 '13 at 19:53
  • Even if Debian Squeeze was released on 2011-02-06, it was frozen on 2010-08-06, before this coreutils change... I'm rather looking for a "standard" solution possible if possible. – Totor Apr 12 '13 at 20:57
  • Well, you could just use the file-system %T command, if that works in old stat. If that doesn't work either and your version of stat does not provide the information you need, back to the awkward solutions it is. – frostschutz Apr 12 '13 at 21:23

mount -l and use grep, sed, or awk to find the line that refers to the directory in question.

  • It looks complicated to figure out that /my/path/to/the/directory is actually something from the mountpoint /my/path with your answer. Maybe you can give some more details about the grep, sed, awk commands you suggest (a complete answer)?. – Totor Apr 12 '13 at 18:16
  • mount -l | grep 'type nfs' | sed 's/.* on \([^ ]*\) .*/\1/' should give you a list of all nfs mountpoints on the system. – Andy Shulman Jun 10 '14 at 16:51
  • This can be combined with stat: mount | fgrep "`stat --printf=" on %m type" /var/log/`". The mount` command will print all mount points, in <device> on <mountpoint> type <type> format. To grep this, I have stat --printf generate the on <mountpoint> type part. – MSalters Aug 27 '15 at 12:45
  • there are many local and remote filesystem mount types. having a map of all of them may be challenging. (for example is drvfs local or remote?) – Erik Aronesty Feb 15 at 17:10
  • Yes, but the question specifically asks about nfs: "mounted remote (nfs) filesystem" – jayhendren Feb 15 at 19:30

Unfortunately, there is no universal Unix answer to that.

One thing you can do, for a given file /a/b/c/d is walk up the path:

  • /a/b/c/.
  • /a/b/c/..
  • /a/b/c/../..
  • ...

... and do a stat(2) at each level, until the st_dev changes. Then you'll know where the mount point is. Then you can look up the canonical path of that mount point in /etc/mtab or in the output of mount to find out the file system type. Then finding out what is remote and what is not is going to be tricky especially for fuse-type ones. For instance, nfs, cifs, fuse.sshfs, fuse.davfs are obvious, but what about for instance fuse.gvfs-fuse-daemon or fuse.avfsd that can have both network and non-network files?

  • Well, I'm using nfs here (edited my question). As for stat(2), I think you are refering the C function, but I'm using shell. However, st_dev refers the to major and minor device ID (is that Linux specific?), and the shell command stat(1) has (non-standard) %t and %T with the --format options for respectively major and minor numbers, but it always returns "0" (local and nfs). I tried on Debian Squeeze and Lenny. – Totor Apr 12 '13 at 21:17
  • Are you trying this with or without the --file-system option? – frostschutz Apr 12 '13 at 22:54
  • @frostschutz Without. It has another meaning if used with --file-system. – Totor Apr 12 '13 at 23:52

The "-l" to df(1) will fail with an error on non-local filesystems, so you can use this behavior to know if the filesystem is remote:

df -l /path 2> /dev/null | grep -q "File"
if [ "$rc" = "0" ]
   echo "local mount, do stuff"

However, the -l option is not standard.

  • This is not an answer – Totor May 2 '17 at 11:38
  • @Totor why not? Granted, there are all sorts of other reasons why df would fail and that alone makes this a not very good answer, but it is trying to answer the question so I don't see why you'd call it "not an answer". – terdon May 2 '17 at 14:25
  • @terdon I agree. I didn't read carefully enough. Thank you for pointing this out. I'll edit and vote up. :) – Totor May 2 '17 at 21:45
  • This is basically the only one that works reliably on linux. – Erik Aronesty Feb 15 at 17:10

df /path will tell you that /path is a mount point if it says that the mount point is not /.

  • 1
    The question refers to a remote mount point. – Totor Jun 24 '13 at 22:26

Just do

df /me/path/to/folder

If the first field (the Filesystem) is in the format host:/path then you know it is NFS


You can use the df -t on Linux and df -T on BSD systems.

From man df:

-t, --type=TYPE

limit listing to file systems of type TYPE

if df -t nfs | grep -q /path/to/folder$; then
  # it is an NFS
  • Pretty nice. However, and unfortunately, the -t option has a different meaning in POSIX/SUS. So, using this option may provide inconsistent results in different versions on the tool/UNIX. – Totor Jun 18 '17 at 19:40

I would personally use mountpoint (very portable on Linux!):

       mountpoint - see if a directory is a mountpoint

       mountpoint [-d|-q] directory

or showmount which is pretty much required to be installed on any system that actually mount NFS shares (part of nfs-common package):

       showmount - show mount information for an NFS server

       showmount [ -adehv ] [ --all ] [ --directories ] [ --exports ] [ --help ] [ --version ] [ host ]

Another option would be someting like:

$ mount -l -t nfs | grep 'my mount point'
  • 1
    The mountpoint utility does not tell you if the mount point is remote. Moreover, remote filesystems are not limited to NFS filesystems. Finally, mountpoint is not standard (not in this list). – Totor Jan 20 '16 at 0:42
  • you specifically requested a tool on linux and NFS, mountpoint is standard in this case. – malat Jan 20 '16 at 9:15
  • This does not answer my first point anyway. – Totor Jan 25 '16 at 15:30
find . -type d -name nfs -exec mountpoint {} \; | grep not

will check all folders named 'nfs' if they're mounted

  • OP is not looking for folders named nfs. – codeforester Jun 6 '17 at 20:46

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.