How can I find the newest created file in a directory?

后端 未结 6 2343
情书的邮戳
情书的邮戳 2020-12-15 05:06

Is there an elegant way in Perl to find the newest file in a directory (newest by modification date)?

What I have so far is searching for the files I need, and for e

6条回答
  •  鱼传尺愫
    2020-12-15 05:53

    Subject is old, but maybe someone will try it - it isn't portable (Unix-like systems only), but it's quite simple and works:

    chdir $directory or die "cannot change directory";

    my $newest_file = bash -c 'ls -t | head -1';

    chomp $newest_file;

    print "$newest_file \n";

提交回复
热议问题