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
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. It only takes a minute to sign up.
Sign up to join this communityAs 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."
/etc/mtab
or results of mount
instead of using df
could do in some more cases.
mount
, nor /etc/mtab
, not even /proc/mounts
are standard so there is no guarantee on what may be found there...
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 | awk
wardness.
stat
does not have the %m
option for --format
(using Debian Squeeze, coreutils 8.5). So, I guess this solution is not very reliable.
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
mount -l
and use grep
, sed
, or awk
to find the line that refers to the directory in question.
/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)?.
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
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.
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:
... 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?
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.
--file-system
.
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.
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".
df /path
will tell you that /path
is a mount point if it says that the mount point is not /
.
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)
The following works fine on CentOS 7.
/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
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
.
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
-t
option has a different meaning in POSIX/SUS. So, using this option may provide inconsistent results in different versions on the tool/UNIX.
Using the findmnt
command should give you exactly the output you're looking for:
$ findmnt --output=FSTYPE -nfT /run/user/1000/kio-fuse-VBkDcY/sftp/hostname/my/path/to/folder
fuse.kio-fuse
The output is simply the filesystem type. In my case, I gave it a file which was mounted remotely in dolphin file manager.
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'
mountpoint
is standard in this case.
find . -type d -name nfs -exec mountpoint {} \; | grep not
will check all folders named 'nfs' if they're mounted