开发者

How do I find out the mode (permissions) of a directory?

How do I find out the mode (permissions) of a directory开发者_如何学C?


According to perldoc -f stat:

$mode = (stat($filename))[2];
printf "Permissions are %04o\n", $mode & 07777;


Other examples require you to know that the mode is third item in stat output ( ie [2] ). File::stat lets you give symbolic name.

use File::stat ;
my $dir = '/etc/cron.d' ;
printf "%o", stat($dir) -> mode ;


my $mode;
(undef, undef, $mode) = stat($directoryname);


Good answers so far. I wish to add an additional good module.

Most of the time, you only want to know the mode of a file so that you can manipulate it afterwards. use Fcntl qw(:mode) or use POSIX qw(:sys_stat_h) export the necessary constants, e.g. S_IXUSR. I find this is unwieldy, even error prone as this is the rare time in Perl where you encounter mathematics with octal numbers and bit operators.

For this purpose, File::chmod has the better interface because it lets you express the change

  • without the need to explicitely query the old mode and calculate the new one,
  • in more familiar ways than octal, namely
    • symbolic, known from chmod(1), e.g. u-x
    • like in ls(1), e.g. -rw-r--r--
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜