如何找到递归目录中的最新修改的文​​件? [英] How to recursively find the latest modified file in a directory?

查看:167
本文介绍了如何找到递归目录中的最新修改的文​​件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

似乎 LS 不这样做递归调用时,文件正确地排序:

  LS -altR。 |头-n 3

我如何才能找到最近修改的文件中的一个目录(包括子目录)?


解决方案

 找到。型的F -printf'%T @%P \\ N'|排序-n |尾-1 |切-f2- -D

有关一个巨大的树,它可能为很难排序来保存在内存中的一切。

%T @ 为您提供了修改时间就像一个UNIX时间戳,排序-n 各种数字,尾-1 取最后一行(最高时间戳),切-f2 -d切掉的第一个字段(从输出时间戳)。

编辑:正如 -printf 可能是GNU只,ajreals 的使用与stat -c 太。虽然可以做同样的BSD的格式化选项是不同的( -f%M%N这似乎)

和我错过了复数的一部分;如果您想了解更多,然后的的最新的文件,只是撞了尾巴的说法。

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\n' | sort -n | tail -1 | cut -f2- -d" "

For a huge tree, it might be hard for sort to keep everything in memory.

%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.

Edit: 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)

And I missed the part of plural; if you want more then the latest file, just bump up the tail argument.

这篇关于如何找到递归目录中的最新修改的文​​件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆