如何递归查找目录中最新修改的文件? [英] How to recursively find the latest modified file in a directory?
问题描述
似乎 ls
在进行递归调用时没有正确排序文件:
It seems that ls
doesn't sort the files correctly when doing a recursive call:
ls -altR . | head -n 3
如何在目录(包括子目录)中找到最近修改的文件?
How can I find the most recently modified file in a directory (including subdirectories)?
推荐答案
find . -type f -printf '%T@ %p
'
| sort -n | tail -1 | cut -f2- -d" "
对于一棵巨大的树,sort
可能很难将所有内容都保存在内存中.
For a huge tree, it might be hard for sort
to keep everything in memory.
%T@
给你修改时间就像一个unix时间戳,sort -n
数字排序,tail -1
取最后一行(最高时间戳),cut -f2 -d""
从输出中去掉第一个字段(时间戳).
%T@
gives you the modification time like a unix timestamp, sort -n
sorts numerically, tail -1
takes the last line (highest timestamp), cut -f2 -d" "
cuts away the first field (the timestamp) from the output.
正如 -printf
可能是 GNU 唯一的一样,stat -c
的 ajreals 用法也是如此.虽然可以在 BSD 上做同样的事情,但格式化的选项是不同的(-f "%m %N"
看起来是这样)
Just as -printf
is probably GNU-only, ajreals usage of stat -c
is too. Although it is possible to do the same on BSD, the options for formatting is different (-f "%m %N"
it would seem)
我错过了复数部分;如果您想要更多 最新文件,只需增加 tail 参数即可.
And I missed the part of plural; if you want more then the latest file, just bump up the tail argument.
这篇关于如何递归查找目录中最新修改的文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!