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

没有看到ls的limit参数…


当前回答

我使用:

ls -ABrt1——group- directory -first | tail -n1 . ls -ABrt1——group- directory -first | tail -n1 . ls

它只给我文件名,不包括文件夹。

其他回答

我喜欢echo *(om[1]) (zsh语法),因为它只给出文件名,而不调用任何其他命令。

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

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

查找/排序解决方案工作得很好,直到文件数量变得非常大(比如整个文件系统)。使用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]* //'
ls -Art | tail -n 1

这将返回最新修改的文件或目录。不是很优雅,但是很好用。

使用国旗:

-A列出所有文件。和. .

-r在排序时倒序

-t按时间排序,latest在前

所有这些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排序。