Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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? Or is the only way to do this to parse /etc/fstab and check the filesystem type?

like image 431
Alex Avatar asked Nov 17 '08 18:11

Alex


People also ask

How do you check if it is a file or directory in Perl?

The quickest way to tell files from directories is to use Perl's built-in ​File Test Operators. Perl has operators you can use to test different aspects of a file. The -f operator is used to identify regular files rather than directories or other types of files.

How do I get the directory path in Perl?

The dirname() method in Perl is used to get the directory of the folder name of a file.

How do I open a file in path in Perl?

To open a file that's in another directory, you must use a pathname. The pathname describes the path that Perl must take to find the file on your system. You specify the pathname in the manner in which your operating system expects it, as shown in the following examples: open(MYFILE, "DISK5:[USER.

How do I read all files in a directory in Perl?

Here's some sample code that will show you how to loop through each file in a directory: $dirname = '. '; opendir(DIR, $dirname) or die "Could not open $dirname\n"; while ($filename = readdir(DIR)) { print "$filename\n"; } closedir(DIR);


3 Answers

stat -f -c %T <filename> should do what you want. You might also want -l

like image 171
Chris Dodd Avatar answered Oct 05 '22 05:10

Chris Dodd


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;}'
like image 26
Steve Baker Avatar answered Oct 05 '22 07:10

Steve Baker


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.

like image 35
Svante Avatar answered Oct 05 '22 06:10

Svante