49

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 

11 Answers 11

37

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."

10
  • 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. Apr 12, 2013 at 22:18
  • 1
    Viewing /etc/mtab or results of mount instead of using df could do in some more cases.
    – TNW
    Apr 16, 2013 at 23:50
  • 1
    @TNW or even /proc/mounts
    – laebshade
    Apr 17, 2013 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, 2013 at 22:44
  • 1
    This not work on OSX 10.8 as -T have a different meaning here
    – DavAlPi
    Jun 23, 2013 at 16:25
19

You could use GNU stat.

%m to find out the mountpoint.

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

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

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

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.

8
  • 1
    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, 2013 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 Apr 12, 2013 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. Apr 12, 2013 at 19:53
  • 1
    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. Apr 12, 2013 at 21:23
  • 1
    Thank you for posting this. In 2021, this answer is a lot more widely applicable than commenters in 2013 concluded. Feb 28, 2021 at 23:27
8

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

5
  • 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, 2013 at 18:16
  • mount -l | grep 'type nfs' | sed 's/.* on \([^ ]*\) .*/\1/' should give you a list of all nfs mountpoints on the system. Jun 10, 2014 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, 2015 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?) Feb 15, 2019 at 17:10
  • Yes, but the question specifically asks about nfs: "mounted remote (nfs) filesystem"
    – jayhendren
    Feb 15, 2019 at 19:30
2

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?

3
  • 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, 2013 at 21:17
  • Are you trying this with or without the --file-system option? Apr 12, 2013 at 22:54
  • @frostschutz Without. It has another meaning if used with --file-system.
    – Totor
    Apr 12, 2013 at 23:52
2

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"
rc=$?
if [ "$rc" = "0" ]
then
   echo "local mount, do stuff"
fi

However, the -l option is not standard.

4
  • This is not an answer
    – Totor
    May 2, 2017 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, 2017 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, 2017 at 21:45
  • This is basically the only one that works reliably on linux. Feb 15, 2019 at 17:10
1

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

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

Do:

df /me/path/to/folder

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

So:

df /my/path/to/folder | awk 'NR==1{next};$1~/:[/]/{print "yes";exit(0)};{print "No";exit(1)}'

Now, put it in a bash file named "ifchk-remote-mountpoint".

Usage:

if ifchk-remote-mountpoint  /my/path/to/folder >/dev/null; then
    ...do something...
fi

or:

ISNFS=$(ifchk-remote-mountpoint  /my/path/to/folder)
0

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
fi
1
  • 1
    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, 2017 at 19:40
0

The following works fine on CentOS 7.

  1. Find the mount point of the required path (/mnt/users/bbharati in my case) with the below command:

df -hk /mnt/users/bbharati

Filesystem                                 1K-blocks       Used  Available Use% Mounted on
svm-dev-01-2048-lif:/expo_dev_users/users 6764573504 3540678720 3223894784  53% /mnt/svmdev01expodevusers
  1. From the output of the above command, pick the value for the Mounted on field, and use that in the below command:

mount -l | grep /mnt/svmdev01expodevusers

svm-dev-01-2048-lif:/expo_dev_users/users on /mnt/svmdev01expodevusers type nfs (rw,relatime,vers=3,rsize=65536,wsize=65536,namlen=255,hard,proto=tcp,timeo=600,retrans=2,sec=sys,mountaddr=1.......)

You can see that in the output of the second command, type is nfs.

-2

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

NAME
       mountpoint - see if a directory is a mountpoint

SYNOPSIS
       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):

NAME
       showmount - show mount information for an NFS server

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

Another option would be someting like:

$ mount -l -t nfs | grep 'my mount point'
3
  • 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, 2016 at 0:42
  • you specifically requested a tool on linux and NFS, mountpoint is standard in this case.
    – malat
    Jan 20, 2016 at 9:15
  • This does not answer my first point anyway.
    – Totor
    Jan 25, 2016 at 15:30
-3
find . -type d -name nfs -exec mountpoint {} \; | grep not

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

1
  • OP is not looking for folders named nfs. Jun 6, 2017 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.