在进行递归调用时,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

其他回答

下面的命令在Solaris上运行:

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

我发现上面的命令很有用,但对于我的情况,我需要查看文件的日期和时间,我有一个问题,几个文件的名称中有空格。 这是我的工作解决方案。

find . -type f -printf '%T@ %p\n' | sort -n | tail -1 | cut -f2- -d" " | sed 's/.*/"&"/' | xargs ls -l

我更喜欢这个,它更短:

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

使用find -具有良好和快速的时间戳

下面介绍如何查找并列出带有子目录的目录中最新修改的文件。隐藏文件被故意忽略。时间格式可以自定义。

$ find . -type f -not -path '*/\.*' -printf '%TY-%Tm-%Td %TH:%TM %Ta %p\n' |sort -nr |head -n 10

结果

处理文件名中的空格非常好-不是说这些应该被使用!

2017-01-25 18:23 Wed ./indenting/Shifting blocks visually.mht
2016-12-11 12:33 Sun ./tabs/Converting tabs to spaces.mht
2016-12-02 01:46 Fri ./advocacy/2016.Vim or Emacs - Which text editor do you prefer?.mht
2016-11-09 17:05 Wed ./Word count - Vim Tips Wiki.mht

More

更多的发现大量以下的链接。

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

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