Home: Fun With Perl: Perl Quizzes - Learn Perl the Fun Way:
Checking File Ownership & Permissions



sleuth
Enthusiast

Dec 13, 2000, 3:24 PM


Views: 50581
Checking File Ownership & Permissions

 
How do you check a file's ownership and permissions?

Slueth



sleuth
Enthusiast

Dec 15, 2000, 8:39 PM


Views: 50570
Re: Checking File Ownership & Permissions

 If anyone wants to know, you can use stat();

$filename = "file.db";
($dev, $ino, $mode, $nlink, $uid, $gid, $rdev, $size, $atime, $mtime, $ctime, $blksize, $blocks) = stat($filename) or die "no $filename: $!";
print "Permission: $mode";

Also, going by the above variable names in the same order, they can be stored in an array for convenience.

@stats = stat($filename);
print "Permission: $stats[2]";

Sleuth



japhy
Enthusiast

Dec 15, 2000, 10:48 PM


Views: 50568
Re: Checking File Ownership & Permissions

You can also use the file test operators, for specific access:


Code
print "I can read $file\n" if -r $file; 
print "I can write to $file\n" if -w _;
print "I can execute $file\n" if -x _;

The _ is a special filehandle which refers to the previous one used ($file, in this case).

Jeff "japhy" Pinyan -- accomplished hacker, teacher, lecturer, and author