-3

我有这个文件

lrwxrwxrwx.  1 user user      32 Sep 20 15:43 SingletonLock -> user.hostname.com-22222

我需要 perl 识别该文件并获取它的大小,我可以使用什么命令?

我努力了:

my $size = (lstat("/home/user/.config/google-chrome/SingletonLock -> user.hostname.com-22222"))[7];
print $size;

但变量是空的并且$!No such file or directory

4

2 回答 2

3

stat功能?或者您可能需要lstat获取链接信息,或readlink读取符号链接指向的名称。

示例statlstat工作:

$ echo "Petunia" > user.hostname.com-22222
$ ln -s user.hostname.com-22222 SingletonLock
$ ls -l user.* Singl*
lrwxr-xr-x  1 jleffler  staff  23 Sep 26 20:24 SingletonLock -> user.hostname.com-22222
-rw-r--r--  1 jleffler  staff   8 Sep 26 20:24 user.hostname.com-22222
$ cat stat.pl
#!/usr/bin/env perl
use strict;
use warnings;

my @names = ( "user.hostname.com-22222", "SingletonLock" );

foreach my $file (@names)
{
    my ($dev,$ino,$mode,$nlink,$uid,$gid,$rdev,$size,
            $atime,$mtime,$ctime,$blksize,$blocks)
        = lstat $file;
    printf "lstat: %2d (%.5o) - %s\n", $size, $mode, $file;
    ($dev,$ino,$mode,$nlink,$uid,$gid,$rdev,$size,
            $atime,$mtime,$ctime,$blksize,$blocks)
        = stat $file;
    printf "stat:  %2d (%.5o) - %s\n", $size, $mode, $file;
}
$ perl stat.pl
lstat:  8 (100644) - user.hostname.com-22222
stat:   8 (100644) - user.hostname.com-22222
lstat: 23 (120755) - SingletonLock
stat:   8 (100644) - SingletonLock
$
于 2013-09-27T02:56:44.130 回答
0

只给 lstat 文件名

my $size = (lstat("/home/user/.config/google-chrome/SingletonLock"))[7];

如果您只想获取文件的大小,可以使用-sperl 中的选项

print -s "/home/user/.config/google-chrome/SingletonLock"; 

或者

$filesize = -s $filename;
于 2013-09-27T05:26:27.563 回答