如何在Perl中检查非空文件

Gra*_*ace 2 perl operators

我正在使用该find命令在目录中查找文件.我想在继续之前检查目录中的文件是否为空(非0大小).感谢find手册,我知道如何使用该-empty选项识别空文件.

但是,我想使用Perl来检查非空文件.我怎样才能做到这一点?

提前致谢.

bob*_*mcr 11

请参阅perldoc perlfunc -XPerl文件测试操作员的复习.你想要的是这个:

-s  File has nonzero size (returns size in bytes).

简单的脚本显示如何使用File::Find:

#!/usr/bin/perl -w
use strict;

use File::Find;

# $ARGV[0] is the first command line argument
my $startingDir = $ARGV[0];

finddepth(\&wanted, $startingDir);

sub wanted
{
    # if current path is a file and non-empty
    if (-f $_ && -s $_)
    {
        # print full path to the console
        print $File::Find::name . "\n";
    }
}
Run Code Online (Sandbox Code Playgroud)

在这个例子中,我将输出发送到控制台.要将它传递给文件,您可以使用shell输出重定向,例如./findscript.pl /some/dir > somefile.out.