Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

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 
share|improve this question

5 Answers 5

up vote 6 down vote accepted

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

share|improve this answer
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
    
@TNW or even /proc/mounts –  laebshade Apr 17 '13 at 3:55
    
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
    
This not work on OSX 10.8 as -T have a different meaning here –  DavAlPi Jun 23 '13 at 16:25

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?

share|improve this answer
    
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

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

share|improve this answer
    
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 at 16:51

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.

share|improve this answer
    
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
    
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

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

share|improve this answer
    
The question refers to a remote mount point. –  Totor Jun 24 '13 at 22:26

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.