Macos 在带有空白的目录中查找上次修改的文件
我发现并调整了这个脚本,以递归方式查找目录中最近修改的文件。它仅在目录名中有空格时才会中断。有人能帮我调整脚本,让它也能读取带有空格的目录吗Macos 在带有空白的目录中查找上次修改的文件,macos,perl,bash,find,Macos,Perl,Bash,Find,我发现并调整了这个脚本,以递归方式查找目录中最近修改的文件。它仅在目录名中有空格时才会中断。有人能帮我调整脚本,让它也能读取带有空格的目录吗 for i in *; do find $i -type f | perl -ne 'chomp(@files = <>); my $p = 9; foreach my $f (sort { (stat($a))[$p] <=> (stat($b))[$p] } @files) { print scalar localtime((
for i in *; do
find $i -type f | perl -ne 'chomp(@files = <>); my $p = 9; foreach my $f (sort { (stat($a))[$p] <=> (stat($b))[$p] } @files) { print scalar localtime((stat($f))[$p]), "\t", $f, "\n" }' | tail -1
done
用于i in*;做
查找$i-typef | perl-ne'chomp(@files=);我的$p=9;foreach my$f(sort{(stat($a))[$p](stat($b))[$p]}@files){print scalar localtime((stat($f))[$p]),“\t”,$f,“\n”}”tail-1
完成
引用可以修复一切
find "$i" -type f
此外,您不需要尾部。只需交换$a
和$b
,然后在打印后退出即可
find $i -type f | perl -lne 'chomp(@files = <>); my $p = 9; foreach my $f (sort { (stat($b))[$p] <=> (stat($a))[$p] } @files) { print scalar localtime((stat($f))[$p]), "\t", $f; exit }'
由于您只处理当前目录,因此只能使用一个命令:
find . -type f | perl -ne 'chomp(@files = <>); my $p = 9; foreach my $f (sort { (stat($a))[$p] <=> (stat($b))[$p] } @files) { print scalar localtime((stat($f))[$p]), "\t", $f, "\n" }' | tail -1
find-键入f | perl-ne'chomp(@files=);我的$p=9;foreach my$f(sort{(stat($a))[$p](stat($b))[$p]}@files){print scalar localtime((stat($f))[$p]),“\t”,$f,“\n”}”tail-1
用Perl编写这一切似乎不那么麻烦
perl -MFile::Find -e 'find(sub{@f=((stat)[9],$File::Find::name) if -f && $f[0]<(stat)[9]},".");print "@f")'
perl-MFile::Find-e'Find(sub{@f=((stat)[9],$File::Find::name)if-f&&$f[0]默认情况下,下面的代码搜索当前工作目录下的子树。您还可以在命令行上命名更多要搜索的子树
#! /usr/bin/env perl
use strict;
use warnings;
use File::Find;
my($newest_mtime,$path);
sub remember_newest {
return if -l || !-f _;
my $mtime = (stat _)[9];
($newest_mtime,$path) = ($mtime,$File::Find::name)
if !defined $newest_mtime || $mtime > $newest_mtime;
}
@ARGV = (".") unless @ARGV;
for (@ARGV) {
if (-d) {
find \&remember_newest, @ARGV;
}
else {
warn "$0: $_ is not a directory.\n";
}
}
if (defined $path) {
print scalar(localtime $newest_mtime), "\t", $path, "\n";
}
else {
warn "$0: no files processed.\n";
exit 1;
}
如前所述,代码不遵循符号链接。如果在命令行中命名符号链接,您将看到
$ ./find-newest ~/link-to-directory
./find-newest: no files processed.
$。/查找最新的~/指向目录的链接
/查找最新文件:未处理任何文件。
对于bash,您必须添加一个尾部斜杠来强制取消引用
$ ./find-newest ~/link-to-directory/
Thu Jan 1 00:00:00 1970 hello-world
$。/查找最新的~/指向目录的链接/
1970年1月1日星期四00:00:00 hello worldPerl?您没有bash,喜欢编写长代码行?;-)
感谢您的快速响应。我肯定会以这种方式尝试,而不使用循环。实际上,我通过在for循环之前添加IFS='\n'解决了此问题。实际上,我认为我自己的解决方案不起作用。再次感谢您的输入,我会尝试让您的建议发挥作用。好的,再次感谢您!添加引号会给我结果我需要.OS X的find没有-printf选项。你可以使用类似于find.-exec stat-f”%m%N{}\|sort-N | tail-1
的东西。啊。是的,我原来的find也有类似的exec用法。相信sort-r-N | head-1
仍然会更有效?(现在开始剪头发!)
$ ./find-newest ~/link-to-directory/
Thu Jan 1 00:00:00 1970 hello-world
find . -type f -printf '%T+ %p\n' | sort -r | head -n1