Get most recent file in a directory on Linux

前端 未结 21 2500
余生分开走
余生分开走 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:01

    This is a recursive version (i.e. it finds the most recently updated file in a certain directory or any of its subdirectory)

    find $DIR -type f -printf "%T@ %p\n" | sort -n | cut -d' ' -f 2- | tail -n 1
    

    Edit: use -f 2- instead of -f 2 as suggested by Kevin

提交回复
热议问题