简体   繁体   中英

How do I determine the newest created file in a directory on a linux machine?

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.

No you can not get the files on the basis of their birth date, as their is no linux command to get the birth date of a file, but of-course you can get the access, modification and change information about the file. To get the access, modification and change time information of any file use this :

stat file-name

Also, to get the most recent changed/modified file use this:

ls -ltr | tail -1

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).

Try:

cd DIR
ls -l -rt | tail -1

The naughty IO::All method: ;-)

use IO::All; 
use v5.20;

# sort files by their modification time and store in an array:
my @files = sort{$b->mtime <=> $a->mtime} io(".")->all_files; 
# get the first/newest file from the sort: 
say "$files[0] ". ~~localtime($files[0]->mtime);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM