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?
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.
The dirname() method in Perl is used to get the directory of the folder name of a file.
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.
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);
stat -f -c %T <filename>
should do what you want. You might also want -l
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;}'
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With