Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/perl/11.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/three.js/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Perl 查找最近修改的文件夹_Perl - Fatal编程技术网

Perl 查找最近修改的文件夹

Perl 查找最近修改的文件夹,perl,Perl,$some_文件夹将始终位于~/foo/bar/中$some_folder始终是~/foo/bar/中最近修改的文件夹,但是,每个系统的名称永远不会相同。如何查找$some_folder?嗯,它被标记为perl,所以我假设您需要一些perl代码。但正如评论中所指出的: ls -1dt */ | head -n 1 我会成功的 在perl中,您需要stat和glob以及可能的sort: #!/usr/bin/env perl use strict; use warnings; my %fil

$some_文件夹
将始终位于
~/foo/bar/
$some_folder
始终是
~/foo/bar/
中最近修改的文件夹,但是,每个系统的名称永远不会相同。如何查找
$some_folder

嗯,它被标记为
perl
,所以我假设您需要一些
perl
代码。但正如评论中所指出的:

ls -1dt */ | head -n 1
我会成功的

perl
中,您需要
stat
glob
以及可能的
sort

#!/usr/bin/env perl

use strict;
use warnings;

my %files_by_mtime;

foreach my $file ( glob ( "~/foo/bar/*" ) ) {
   next unless -d $file;
   my ( $mtime ) = (stat($file))[9];
   $files_by_mtime{$file} = $mtime; 
}

foreach my $filename (  sort { $files_by_mtime{$b} <=> $files_by_mtime{$a} } keys %files_by_mtime ) {
   print "$filename  $files_by_mtime{$filename }\n";
}
#/usr/bin/env perl
严格使用;
使用警告;
我的%files\u按\u时间;
foreach my$文件(glob(“~/foo/bar/*”){
下一个-d$文件;
my($mtime)=(stat($file))[9];
$files_by_mtime{$file}=$mtime;
}
foreach my$filename(按\u-mtime对{$files\u{$b}$files\u按\u-mtime{$a}}键对%files\u按\u-mtime排序){
打印“$filename$files\u按\u mtime{$filename}\n”;
}
生成按mtime排序的列表

为简化此过程,您只需访问元素零:

print "".(  sort { $files_by_mtime{$b} <=> $files_by_mtime{$a} } keys %files_by_mtime )[0],"\n";
print”“(按时间对{$files{u排序{$b}$files{u排序{$a}}键%files{u排序)[0],“\n”;
或者简单地说一句话:

perl -e '%f = map { -d ? $_ => (stat)[9] : () } glob("~/foo/bar/*");print "" . ( sort { $f{$b} <=> $f{$a} } keys %f )[0];
perl-e“%f=map{-d?$\=>(stat)[9]:()}glob(“~/foo/bar/*”);打印“”。(排序{$f{$b}$f{$a}}键%f)[0];

ls-1dt*/| head-n1
将为您提供最近修改的文件夹。欢迎使用堆栈溢出。请花点时间阅读。谢谢!成功了。