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

后端 未结 6 2347
情书的邮戳
情书的邮戳 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:54

    If you can't let ls do the sorting for you as @Nathan suggests, then you can optimize your process by only keeping the newest modification time and associated filename seen thus far and replace it every time you find a newer file in the directory. No need to keep any files around that you know are older than the newest one you've seen so far and certainly no need to sort them since you can detect which is the newest one while reading from the directory.

提交回复
热议问题