如何在 Perl 中检查非空文件
我使用 find
命令在目录中查找文件。我想在继续之前检查目录中的文件是否不为空(非 0 大小)。感谢 find
手册,我知道如何使用 -empty
选项识别空文件。
但是,我想使用 Perl 来检查非空文件。我怎样才能做到这一点?
提前致谢。
I'm using the find
command for finding files in directories. I would like to check if the files in the directories are not empty (non 0 size) before proceeding. Thanks to the find
manual, I know how to identify empty files using the -empty
option.
However, I want to use Perl to check for non-empty files. How can I do that?
Thanks in advance.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

发布评论
评论(2)
南风几经秋2024-12-30 03:08:54
请查看 perldoc http://perldoc.perl.org/functions/-X。 html
-z File has zero size (is empty).
-s File has nonzero size (returns size in bytes).
检测非空文件的示例用法:
unless ( (-z $FILE) ) { process_file($FILE); }
if (-s $FILE) { process_file($FILE); }
~没有更多了~
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
请参阅
perldoc perlfunc -X
了解Perl 文件测试运算符。您想要的是这个:显示如何使用
File::Find
的简单脚本:在此示例中,我将输出发送到控制台。要将其通过管道传输到文件,您只需使用 shell 输出重定向,例如
./findscript.pl /some/dir > somefile.out
。Refer to
perldoc perlfunc -X
for a refresher of the Perl file test operators. What you want is this one:Simple script showing how to use
File::Find
:In this example I have the output going to the console. To pipe it to a file, you can just use shell output redirection, e.g.
./findscript.pl /some/dir > somefile.out
.