在进行递归调用时,ls似乎没有正确地对文件进行排序:
ls -altR . | head -n 3
如何在目录(包括子目录)中找到最近修改的文件?
在进行递归调用时,ls似乎没有正确地对文件进行排序:
ls -altR . | head -n 3
如何在目录(包括子目录)中找到最近修改的文件?
当前回答
我费了好大劲才找到Solaris 10下最后一个修改过的文件。find没有printf选项,stat不可用。我发现下面这个方法对我来说很管用:
find . -type f | sed 's/.*/"&"/' | xargs ls -E | awk '{ print $6," ",$7 }' | sort | tail -1
要显示文件名,请使用
find . -type f | sed 's/.*/"&"/' | xargs ls -E | awk '{ print $6," ",$7," ",$9 }' | sort | tail -1
解释
找到。-type f查找并列出所有文件 sed的s /。*/"&"/'将路径名用引号括起来以处理空白 xargs ls -E将带引号的路径发送到ls, -E选项确保返回完整的时间戳(格式为年-月-日小时-分-秒-纳秒) Awk '{print $6," ",$7}'只提取日期和时间 Awk '{print $6," ",$7," ",$9}'提取日期,时间和文件名 Sort返回按日期排序的文件 Tail -1只返回最后修改的文件
其他回答
下面的命令在Solaris上运行:
find . -name "*zip" -type f | xargs ls -ltr | tail -1
如果在每个文件上单独运行stat会变慢,你可以使用xargs来加快速度:
find . -type f -print0 | xargs -0 stat -f "%m %N" | sort -n | tail -1 | cut -f2- -d" "
使用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
更多的发现大量以下的链接。
这个简单的cli也可以工作:
ls -1t | head -1
您可以将-1更改为您想要列出的文件数量
接着@plundra的回答,下面是BSD和OS X版本:
find . -type f -print0 \
| xargs -0 stat -f "%m %N" \
| sort -rn | head -1 | cut -f2- -d" "