Get most recent file in a directory on Linux

2019-01-05 09:05发布

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

Not seeing a limit parameter to ls...

18条回答
别忘想泡老子
2楼-- · 2019-01-05 09:29
ls -t | head -n1

This command actually gives the latest modified file in the current working directory.

查看更多
Deceive 欺骗
3楼-- · 2019-01-05 09:30

try this simple command

ls -ltq  <path>  | head -n 1

If you want file name - last modified, path = /ab/cd/*.log

If you want directory name - last modified, path = /ab/cd/*/

查看更多
该账号已被封号
4楼-- · 2019-01-05 09:30

I needed to do it too, and I found these commands. these work for me:

If you want last file by its date of creation in folder(access time) :

ls -Aru | tail -n 1  

And if you want last file that has changes in its content (modify time) :

ls -Art | tail -n 1  
查看更多
\"骚年 ilove
5楼-- · 2019-01-05 09:33
ls -t -1 | sed '1q'

Will show the last modified item in the folder. Pair with grep to find latest entries with keywords

ls -t -1 | grep foo | sed '1q'
查看更多
▲ chillily
6楼-- · 2019-01-05 09:37

using R recursive option .. you may consider this as enhancement for good answers here

ls -arRtlh | tail -50
查看更多
我命由我不由天
7楼-- · 2019-01-05 09:41

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

查看更多
登录 后发表回答