如何确定linux机器上目录中最新创建的文件?(How do I determine the newest created file in a directory on a linux machine

编程入门 行业动态 更新时间:2024-10-27 09:45:17
如何确定linux机器上目录中最新创建的文件?(How do I determine the newest created file in a directory on a linux machine?)

我试图找出一种方法来确定目录中创建的最新文件。 我不能使用模块而且我在Linux操作系统上。

I am trying to figure out a way to determine the most recent file created in a directory. I cannot use a module and I am on a Linux OS.

最满意答案

只是一个简单的谷歌给了我很好的答案

@list = `ls -t`; $newest = $list[0];

或完全在perl

opendir(my $DH, $DIR) or die "Error opening $DIR: $!"; my %files = map { $_ => (stat("$DIR/$_"))[9] } grep(! /^\.\.?$/, readdir($DH)); closedir($DH); my @sorted_files = sort { $files{$b} <=> $files{$a} } (keys %files);

$ sorted_files [0]是最近修改过的。 如果它不是实际的感兴趣文件,您可以遍历@sorted_files,直到找到有趣的文件。

just a simple google gave me good answer

@list = `ls -t`; $newest = $list[0];

or completely in perl

opendir(my $DH, $DIR) or die "Error opening $DIR: $!"; my %files = map { $_ => (stat("$DIR/$_"))[9] } grep(! /^\.\.?$/, readdir($DH)); closedir($DH); my @sorted_files = sort { $files{$b} <=> $files{$a} } (keys %files);

$sorted_files[0] is the most-recently modified. If it isn't the actual file-of-interest, you can iterate through @sorted_files until you find the interesting file(s).

更多推荐

本文发布于:2023-07-29 15:45:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1317559.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:文件   目录中   最新   机器上   linux

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!