linux - 开发一个程序来分析 Linux 磁盘的目录结构并识别任何大于 500kbytes 的文件

标签 linux perl unix methods rule

我正在尝试开发一个程序来分析 Linux 磁盘的目录结构并识别任何大于 500 KB 的文件。

#!/usr/bin/perl

use File::Find::Rule;
use warnings;

my $filelist;

sub buildFile {
    open ($filelist, ">", "filelist.txt") || die $!;

# File find rule and # Provide specific list of directories to scan
    my $SubDirs= File::Find::Rule->directory->in('etc', 'dev', 'bin'); 


    # interpret Size Method and stored the list on @files 
    my @files = File::Find::Rule->size('500')->in($SubDirs);

print $filelist map { "$_\n" } @files;
return \$filelist;

最佳答案

你可以这样写:)

use File::Find::Rule qw/ find rule /;
my @files = find( size => '>500Ki' , in => [ 'etc', 'dev', 'bin' ] );

或迭代器版本(如果文件列表可能很大)

my $rule = rule( size => '>500Ki' )->start( 'etc', 'dev', 'bin' );
while ( defined ( my $file = $rule->match ) ) {
    print $filelist "$file\n";
}

我使用 find() 返回文件列表,使用 rule() 返回其他内容……但它们是一样的

更新:拼写错误修复(根据 Number::Compare,Ki 不是 Kib)和测试程序

#!/usr/bin/perl --
use strict; use warnings;
use Data::Dump qw/ dd /;
use Path::Tiny qw/ path tempdir cwd /;
use File::Find::Rule qw/ find rule /;
Main( @ARGV );
exit( 0 );
sub Main {
    my $temp = tempdir( CLEANUP => 1 );
    my $cwd = cwd();
    chdir $temp;
    makeThem( $temp );
    findThem( );
    chdir $cwd;
#~     $temp->remove_tree;
}
sub makeThem {
    my( $temp ) = @_;
    for my $bed ( qw/ bin etc dev / ){
        path( $temp, $bed )->mkpath;
        path( $temp, $bed, 'one' )->touch;
        path( $temp, $bed, 'two' )->touch;
        path( $temp, $bed, 'tri' )->spew(1x(1024*501));
    }
}
sub findThem {    
#~     my @files = find( size => '>500Kib' , in => [ 'etc', 'dev', 'bin' ] );
    my @files = find( size => '>500Ki' , in => [ 'etc', 'dev', 'bin' ] );
    dd( \@files );
    my $rule = rule( size => '>500Ki' )->start( 'etc', 'dev', 'bin' );
    while ( defined ( my $file = $rule->match ) ) {
        dd( $file );
    }
    dd( find( file => in => [ 'etc', 'dev', 'bin' ] ) );    
}

关于linux - 开发一个程序来分析 Linux 磁盘的目录结构并识别任何大于 500kbytes 的文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23354967/

相关文章:

Linux 2.6.31 调度程序和多线程作业

regex - 如何检测perl字符串中的某些特殊字符?

csv - 使用 Perl 删除字符串末尾的尾随逗号

linux - linux 命令末尾的 "&"是什么意思?

ios - 如何获取命令行工具的包 ID

c++ - 导入文件存在到项目 eclipse C++ Linux

c - 在c中包装一个读取宏

php - 需要跨 2 个负载平衡服务器上传单个文件

python - 根据模式将一个文件拆分为多个文件(切割可以发生在行内)

c - 如何在 go 中将字节转换为 struct(c struct)?