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

没有看到ls的limit参数…


当前回答

所有这些ls/tail解决方案都非常适用于目录中的文件—忽略子目录。

为了在搜索中包含所有文件(递归地),可以使用find。Gioele建议对格式化的查找输出进行排序。但是要小心使用空格(他的建议不适用于空格)。

这应该适用于所有文件名:

find $DIR -type f -printf "%T@ %p\n" | sort -n | sed -r 's/^[0-9.]+\s+//' | tail -n 1 | xargs -I{} ls -l "{}"

这一分类由时,见人发现:

%Ak    File's  last  access  time in the format specified by k, which is either `@' or a directive for the C `strftime' function.  The possible values for k are listed below; some of them might not be available on all systems, due to differences in `strftime' between systems.
       @      seconds since Jan. 1, 1970, 00:00 GMT, with fractional part.
%Ck    File's last status change time in the format specified by k, which is the same as for %A.
%Tk    File's last modification time in the format specified by k, which is the same as for %A.

用%C替换%T,按ctime排序。

其他回答

ls -t | head -n1

这个命令实际上给出了当前工作目录中最新修改的文件或目录。

试试这个简单的命令

ls -ltq  <path>  | head -n 1

如果需要文件名-最后修改,path = /ab/cd/*.log

如果要修改目录名称-最后一次修改,则path = /ab/cd/*/

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

#!/bin/bash -e

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

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

ls -lAtr | tail -1

其他解决方案不包括以“。”开头的文件。

该命令还包括'。'和'..,这可能是也可能不是你想要的:

Ls -latr | tail -1

找到前缀为shravan*的文件并查看

less $(ls -Art shravan* | tail -n 1)