Get most recent file in a directory on Linux

前端 未结 21 2479
余生分开走
余生分开走 2020-11-27 09:24

Looking for a command that will return the single most recent file in a directory.

Not seeing a limit parameter to ls...

21条回答
  •  悲哀的现实
    2020-11-27 09:42

    I personally prefer to use as few not built-in bash commands as I can (to reduce the number of expensive fork and exec syscalls). To sort by date the ls needed to be called. But using of head is not really necessary. I use the following one-liner (works only on systems supporting name pipes):

    read newest < <(ls -t *.log)
    

    or to get the name of the oldest file

    read oldest < <(ls -rt *.log)
    

    (Mind the space between the two '<' marks!)

    If the hidden files are also needed -A arg could be added.

    I hope this could help.

提交回复
热议问题