Is there a quick-and-dirty way to tell programmatically, in shell script or in Perl, whether a path is located on a remote filesystem (nfs or the like) or a local one? Or is the only way to do this to parse /etc/fstab and check the filesystem type?
Asked
Active
Viewed 3,779 times
5 Answers
13
stat -f -c %T <filename>
should do what you want. You might also want -l

Chris Dodd
- 2,920
- 15
- 10
-
2Again, not portable to Solaris (though I have the command on the machine, it is not standard issue). – Jonathan Leffler Nov 17 '08 at 19:30
2
You can use "df -T" to get the filesystem type for the directory, or use the -t option to limit reporting to specific types (like nfs) and if it comes back with "no file systems processed", then it's not one of the ones you're looking for.
df -T $dir | tail -1 | awk '{print $2;}'

Steve Baker
- 4,323
- 1
- 20
- 15
-
1Not portable - 'df -T' does not work on Solaris, for instance. – Jonathan Leffler Nov 17 '08 at 19:28
-
If all network filesystems use host:path semantics for the device name, then I suppose you can check for : in the device name or something to that effect then. – Steve Baker Nov 17 '08 at 20:28
2
If you use df
on a directory to get info only of the device it resides in, e.g. for the current directory:
df .
Then, you can just parse the output, e.g.
df . | tail -1 | awk '{print $1}'
to get the device name.

Svante
- 50,694
- 11
- 78
- 122
1
I have tested the following on solaris7,8,9 & 10 and it seems to be reliable
/bin/df -g <filename> | tail -2 | head -1 | awk '{print $1}'
Should give you have the fs type rather than trying to match for a "host:path" in your mount point.

Vagnerr
- 2,977
- 3
- 33
- 46
0
On some systems, the device number is negative for NFS files. Thus,
print "remote" if (stat($filename))[0] < 0

Leon Timmermans
- 30,029
- 2
- 61
- 110