寻找返回目录中最新文件的命令。
没有看到ls的limit参数…
寻找返回目录中最新文件的命令。
没有看到ls的limit参数…
当前回答
根据模式在每个目录中查找最新的文件,例如工作目录的子目录的名称以“tmp”结尾(不区分大小写):
find . -iname \*tmp -type d -exec sh -c "ls -lArt {} | tail -n 1" \;
其他回答
递归地:
find $1 -type f -exec stat --format '%Y :%y %n' "{}" \; | sort -nr | cut -d: -f2- | head
我使用:
ls -ABrt1——group- directory -first | tail -n1 . ls -ABrt1——group- directory -first | tail -n1 . ls
它只给我文件名,不包括文件夹。
我个人更喜欢使用尽可能少的非内置bash命令(以减少昂贵的fork和exec系统调用的数量)。要按日期排序,需要调用ls。但使用头部是没有必要的。我使用以下一行代码(只适用于支持名称管道的系统):
read newest < <(ls -t *.log)
或者获取最古老的文件的名称
read oldest < <(ls -rt *.log)
(注意两个“<”符号之间的空格!)
如果还需要隐藏文件,可以添加一个参数。
我希望这能有所帮助。
查找/排序解决方案工作得很好,直到文件数量变得非常大(比如整个文件系统)。使用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" \;