简体   繁体   English

如何判断文件是否在Perl的远程文件系统上?

[英]How can I tell if a file is on a remote filesystem with Perl?

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? 在shell脚本或Perl中,是否有一种快速而简单的方式来判断路径是位于远程文件系统(nfs等)还是本地路径? Or is the only way to do this to parse /etc/fstab and check the filesystem type? 或者是唯一的方法来解析/ etc / fstab并检查文件系统类型?

stat -f -c %T <filename> should do what you want. stat -f -c %T <filename>应该做你想要的。 You might also want -l 你可能也想要-l

您可以使用“df -T”获取目录的文件系统类型,或使用-t选项将报告限制为特定类型(如nfs),如果它返回“没有处理文件系统”,那么它不是一个你要找的那些。

df -T $dir | tail -1 | awk '{print $2;}'

If you use df on a directory to get info only of the device it resides in, eg for the current directory: 如果在目录上使用df来获取它所驻留的设备的信息,例如当前目录:

df .

Then, you can just parse the output, eg 然后,您可以解析输出,例如

df . | tail -1 | awk '{print $1}'

to get the device name. 获取设备名称。

I have tested the following on solaris7,8,9 & 10 and it seems to be reliable 我在solaris7,8,9和10上测试了以下内容,它似乎是可靠的

/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. 应该给你有fs类型,而不是尝试匹配你的挂载点中的“host:path”。

On some systems, the device number is negative for NFS files. 在某些系统上,NFS文件的设备编号为负数。 Thus, 从而,

print "remote" if (stat($filename))[0] < 0

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM