【问题标题】:Count the number of files in a folder by date按日期计算文件夹中的文件数
【发布时间】:2019-09-28 04:55:56
【问题描述】:

我需要使用 Perl 按日期计算文件夹中的文件数。这是我的代码:

my $datestring = localtime(time); 
print "$datestring\n"; 
sub demFile { 
    $dir = "/root/perl/*"; 
    my @file = glob($dir); 
    foreach(@file) { 
        $a = scalar(grep $_, @file) . "\n"; 
        return $a; 
    } 
}

$b = demFile(); 
print $b;

【问题讨论】:

  • grep $_, @file 的目的是什么?似乎是@file 上的身份运算符。例如grep { $_ } @lst 会返回相同的@lst
  • @HåkonHægland grep $_, @file 不是身份运算符。例如,perl -E 'say join "-", grep $_, 1, 0, 2, "a", "", "c"'。但是,我不确定它在该问题中的用途。
  • @Dada 好点!
  • 你可能在想map $_, LIST。但即使这样也不完全等同于LIST。它创建元素的副本。这意味着$_=uc($_) for @a;$_=uc($_) for map $_, @a; 是不同的。如果您想要一个不创建副本的列表操作,则必须使用 grep 1, LISTsub :lvalue { @_ }->(LIST)
  • 非常感谢!这是我的新代码:使用 Time::localtime; $日期=本地时间();打印$日期;打印“\n”; $dir = "/root/perl/*";我的@file = glob($dir); $大小=@文件; foreach (我的 $i=0; $i

标签: file perl count directory


【解决方案1】:

与 Kjetil S. 的解决方案一样,此解决方案接受文件名而不是目录名称,以提供比您要求的解决方案更灵活的解决方案。

此版本解决了 Kjetil S. 的一些问题。

  • 它提供了与使用工具链的理念一致的简约输出。
  • 它不会对每个文件执行两个统计信息。
  • 它不会跳过非目录、非普通文件。
  • 它处理错误。
  • 更易于阅读。

示例用法:

\ls                    | num_files_by_date
\ls /some/dir          | num_files_by_date
\ls -d /some/dir/*.txt | num_files_by_date
find dir1 dir2         | num_files_by_date

代码:

#!/usr/bin/perl
use strict;
use warnings;

use File::stat qw( stat );
use POSIX qw( strftime );

my %counts_by_date;
while (<>) {
    chomp;
    my $stat = stat($_)
       or do {
          warn("Can't stat \"$_\": $!\n");
          next;
       };

    ++$counts_by_date{ strftime("%F", localtime($stat->mtime)) }
       if !-d $stat;  # Don't count dirs.
}

printf("%s %s\n", $_, $counts_by_date{$_})
   for sort keys %counts_by_date;

【讨论】:

  • 非常感谢!这是我的新代码:使用 Time::localtime; $日期=本地时间();打印$日期;打印“\n”; $dir = "/root/perl/*";我的@file = glob($dir); $大小=@文件; foreach (我的 $i=0; $i
【解决方案2】:
#!/usr/bin/perl
use warnings; use strict;
my($n,%n)=(0);
while(<>){
    chomp;
    next if !-f$_; #just count files, ignore dirs, symlinks etc
    my $mtime=(stat($_))[9];
    my @lt=localtime($mtime); $lt[5]+=1900; $lt[4]+=1;
    my $date=sprintf"%04d-%02d-%02d", @lt[5,4,3];
    $n{$date}++;
    $n++;
}
my @d=sort keys %n;
printf "Date $_ has %3d files\n",$n{$_} for @d;
print "A total of $n files between $d[0] and $d[-1]\n";

另存为num_files_by_date.plchmod +x num_files_by_date.pl。该程序获取 STDIN 上的文件列表并计算每个日期的数量。像这样运行:

\ls -1 | ./num_files_by_date.pl
\ls -d1 /root/perl/* | perl num_files_by_date.pl
find dir1/ dir2/ | ./num_files_by_date.pl

输出可能是:

Date 2019-04-28 has   2 files
Date 2019-04-30 has   3 files
Date 2019-05-03 has   1 files
Date 2019-05-06 has   4 files
A total of 10 files between 2019-04-28 and 2019-05-06

【讨论】:

  • 非常感谢!这是我的新代码:使用 Time::localtime; $日期=本地时间();打印$日期;打印“\n”; $dir = "/root/perl/*";我的@file = glob($dir); $大小=@文件; foreach (我的 $i=0; $i
  • find /dire/ctory/some/where -type f -daystart -mtime -2 -mtime +0 | wc -l
猜你喜欢
  • 2016-02-25
  • 1970-01-01
  • 1970-01-01
  • 2019-02-28
  • 1970-01-01
  • 2017-12-25
  • 1970-01-01
  • 2013-02-22
  • 1970-01-01
相关资源
最近更新 更多