gpt4 book ai didi

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

转载 作者:塔克拉玛干 更新时间:2023-11-02 23:54:09 24 4
gpt4 key购买 nike

我正在尝试开发一个程序来分析 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/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com