寻找返回目录中最新文件的命令。

没有看到ls的limit参数…


当前回答

根据模式在每个目录中查找最新的文件,例如工作目录的子目录的名称以“tmp”结尾(不区分大小写):

find . -iname \*tmp -type d -exec sh -c "ls -lArt {} | tail -n 1" \;

其他回答

使用R递归选项..你可以认为这是对好的答案的增强

ls -arRtlh | tail -50

如果你想在/apps/test目录中找到最后修改的文件夹名称,那么你可以在批处理脚本中放入下面的代码片段并执行它,它将打印最后修改的文件夹名称。

#!/bin/bash -e

export latestModifiedFolderName=$(ls -td /apps/test/*/ | head -n1)

echo Latest modified folder within /apps/test directory is $latestModifiedFolderName

查找/排序解决方案工作得很好,直到文件数量变得非常大(比如整个文件系统)。使用awk来跟踪最近的文件:

find $DIR -type f -printf "%T@ %p\n" | 
awk '
BEGIN { recent = 0; file = "" }
{
if ($1 > recent)
   {
   recent = $1;
   file = $0;
   }
}
END { print file; }' |
sed 's/^[0-9]*\.[0-9]* //'

根据模式在每个目录中查找最新的文件,例如工作目录的子目录的名称以“tmp”结尾(不区分大小写):

find . -iname \*tmp -type d -exec sh -c "ls -lArt {} | tail -n 1" \;

假设您不关心以a开头的隐藏文件。

ls -rt | tail -n 1

否则

ls -Art | tail -n 1