ディレクトリツリーに次のタイムスタンプがあるとします。
root
|__ parent1
| |__ 2021
| | |__ 01
| | | |__ 22
| | | | |__ 12H
| | | | | |__ file1
| | | | | |__ file2
| | | | |__ 13H
| | | | | |__ file1
| | | | | |__ file2
| | | |__ 23
| | | | |__ 12H
| | | | | |__ file1
| | | | | |__ file2
| | | | |__ 13H
| | | | | |__ file1
| | | | | |__ file2
|__ parent2
| |__ etc
私が望むのは、このフォルダ構造を再帰的に進め、各フォルダなどにparent1
見つかったparent2
最新のタイムスタンプを含むファイルの数と一緒に表示することです。たとえば、次のようになります。
PARENT | LAST_TIMESTAMP | COUNT |
--------------------------------------------
parent1 | 2021-01-23T13:00:00 | 2 |
parent2 | 2022-01-01T00:00:00 | 5 | (dummy example)
... ... ...
他の答えも見てきましたが、すべてすべてのフォルダにあるファイルの変更日のみを考慮します。この場合は、フォルダ名にのみ関連します。
ベストアンサー1
使用find
とperl
1行:
タブを使用してタイムスタンプとファイル名を区切り、NULを使用して各レコードを区切ります。したがって、改行文字を含むファイル名を含むすべてのファイル名で機能します。
find .. -type f -printf '%T@\t%p\0' |
perl -MDate::Format -0ne '
($t,$f) = split /\t/,$_,2;
(undef,$p) = split "/", $f;
$T{$p} = $t if ($t > $T{$p});
$count{$p}++;
END {
my $fmt = "%-20s | %-19s | %5s |\n";
printf "$fmt", "PARENT", "LAST_TIMESTAMP", "COUNT";
print "-" x 52, "\n";
foreach (sort keys %T) {
printf $fmt, $_, time2str("%Y-%m-%dT%H:%M:%S",$T{$_}), $count{$_}
}
}'
次の出力が生成されます。
PARENT | LAST_TIMESTAMP | COUNT |
---------------------|---------------------|-------|
foo | 2021-07-16T22:54:22 | 4 |
bar | 2021-06-29T12:25:06 | 13 |
baz | 2021-07-14T14:31:43 | 5 |
quux | 2021-07-16T19:46:21 | 7 |
またはPerlを使用している場合ファイル::検索find
モジュールでは、出力をパイプで接続する必要はありません。
#!/usr/bin/perl
use strict;
use Date::Format;
use File::Find;
my %T; # hash containing newest timestamp for each top-level dir
my %count; # count of files in each top-level dir
find(\&wanted, @ARGV);
my $fmt = "| %-20s | %-19s | %5s |\n";
my $hfmt = "|-%-20s-|-%-19s-|-%5s-|\n";
#print "-" x 54, "\n";
printf "$fmt", "PARENT", "LAST_TIMESTAMP", "COUNT";
printf $hfmt, "-" x 20, "-" x 19, "-" x 5;
foreach (sort keys %T) {
printf $fmt, $_, time2str("%Y-%m-%dT%H:%M:%S", $T{$_}), $count{$_}
}
#print "-" x 54, "\n";
sub wanted {
return unless -f $File::Find::name;
# uncomment only one of the following statements:
# get the mod time of the file itself
my $t = (stat($File::Find::name))[9];
# get the mod time of the directory it's in
#my $t = (stat($File::Find::dir))[9];
my $p = $File::Find::dir;
$p =~ s:^\.*/::;
$T{$p} = $t if ($t > $T{$p});
$count{$p}++;
};
たとえば、find-latest.pl
make実行可能ファイルを使用してそれを保存し、chmod +x find-latest.pl
実行時に引数として1つ以上のディレクトリを指定します。
$ ./find-latest.pl ../
| PARENT | LAST_TIMESTAMP | COUNT |
|----------------------|---------------------|-------|
| foo | 2021-07-16T22:54:22 | 4 |
| bar | 2021-06-29T12:25:06 | 13 |
| baz | 2021-07-14T14:31:43 | 5 |
| quux | 2021-07-16T19:46:21 | 7 |
これはパールが必要です日付形式
基準寸法。 Debianではapt-get install libtimedate-perl
。cpan
strftime()
または、Perlに含まれるコアモジュールであるPOSIXモジュールの機能を使用することもできます。
File::Find
また、Perlに含まれるコアPerlモジュールです。