Get most recent file in a directory on Linux

前端 未结 21 2393
余生分开走
余生分开走 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 10:03

    With only Bash builtins, closely following BashFAQ/003:

    shopt -s nullglob
    
    for f in * .*; do
        [[ -d $f ]] && continue
        [[ $f -nt $latest ]] && latest=$f
    done
    
    printf '%s\n' "$latest"
    

提交回复
热议问题