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

ls -altR . | head -n 3

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


当前回答

这将递归地将当前目录中所有目录的修改时间更改为每个目录中的最新文件:

for dir in */; do find $dir -type f -printf '%T@ "%p"\n' | sort -n | tail -1 | cut -f2- -d" " | xargs -I {} touch -r {} $dir; done

其他回答

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

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

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

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

我更喜欢这个,它更短:

find . -type f -print0|xargs -0 ls -drt|tail -n 1

显示带有人类可读时间戳的最新文件:

find . -type f -printf '%TY-%Tm-%Td %TH:%TM: %Tz %p\n'| sort -n | tail -n1

结果如下所示:

2015-10-06 11:30: +0200 ./foo/bar.txt

若要显示更多文件,请将-n1替换为更高的数字

查询/target_directory目录下的所有子目录下,在过去60分钟内被修改过的文件。

$ find /target_directory -type f -mmin -60

要查找最近修改的文件,按更新时间的相反顺序排序(即,最近更新的文件在前面):

$ find /etc -type f -printf '%TY-%Tm-%Td %TT %p\n' | sort -r