ԱŮװ
Ů
׷
Դװ
˼Դ
Ҿ̳
Ա
װ
װ
83ʱŮ
Ҿװ
Ա̳
Ա̳ Ҿװ Ҿ̳ 83ʱŮ ԱŮװ Ů ׷ Դװ ˼Դ װ Ա װ

存档

文章标签 ‘stat’

perl里的stat函数

2009年9月9日 4 条评论

在perl里,使用stat函数可以得到一个文件的所有属性,下面是perldoc里stat的一个列表
0 dev device number of filesystem
1 ino inode number
2 mode file mode (type and permissions)
3 nlink number of (hard) links to the file
4 uid numeric user ID of file’s owner
5 gid numeric group ID of file’s owner
6 rdev the device identifier (special files only)
7 size total size of file, in bytes
8 atime last access time in seconds since the epoch
9 mtime last modify time in seconds since the epoch
10 ctime inode change time in seconds since the epoch (*)
11 blksize preferred block size for file system I/O
12 blocks actual number of blocks allocated

stat函数的使用很简单,可以像下面这样,打印出所有属性:

1
2
3
4
5
6
7
8
9
#!/usr/bin/perl 
use strict;
use warnings;
 
my $filename = "/tmp/stat.pl";
for(0..12){
    my $d = (stat($filename))[$_];
    print "$d\n";
}

或者也可以使用File::stat模块:

1
2
3
4
use File::stat;
my $b = stat($filename);
printf "File is %s, size is %s, perm %04o, mtime %s\n",
        $filename, $b->size, $b->mode & 07777, scalar localtime $b->mtime;
分类: perl 标签: ,