在进行递归调用时,ls似乎没有正确地对文件进行排序:

ls -altR . | head -n 3

如何在目录(包括子目录)中找到最近修改的文件?


当前回答

我为这个问题写了一个pypi/github包,因为我也需要一个解决方案。

https://github.com/bucknerns/logtail

安装:

pip install logtail

用法:尾部更改文件

logtail <log dir> [<glob match: default=*.log>]

用法2:在编辑器中打开最近更改的文件

editlatest <log dir> [<glob match: default=*.log>]

其他回答

这个简单的cli也可以工作:

ls -1t | head -1

您可以将-1更改为您想要列出的文件数量

如果在每个文件上单独运行stat会变慢,你可以使用xargs来加快速度:

find . -type f -print0 | xargs -0 stat -f "%m %N" | sort -n | tail -1 | cut -f2- -d" " 

我为这个问题写了一个pypi/github包,因为我也需要一个解决方案。

https://github.com/bucknerns/logtail

安装:

pip install logtail

用法:尾部更改文件

logtail <log dir> [<glob match: default=*.log>]

用法2:在编辑器中打开最近更改的文件

editlatest <log dir> [<glob match: default=*.log>]

接着@plundra的回答,下面是BSD和OS X版本:

find . -type f -print0 \
| xargs -0 stat -f "%m %N" \
| sort -rn | head -1 | cut -f2- -d" "

下面的命令在Solaris上运行:

find . -name "*zip" -type f | xargs ls -ltr | tail -1