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

没有看到ls的limit参数…


当前回答

如果你想获得最近修改的文件,也包括任何子目录,你可以用这个小的联机程序:

find . -type f -exec stat -c '%Y %n' {} \; | sort -nr | awk -v var="1" 'NR==1,NR==var {print $0}' | while read t f; do d=$(date -d @$t "+%b %d %T %Y"); echo "$d -- $f"; done

如果你想对已更改的文件做同样的事情,但对于已访问的文件,你只需更改

参数%Y从stat命令到%X。对于最近访问的文件,你的命令是这样的:

find . -type f -exec stat -c '%X %n' {} \; | sort -nr | awk -v var="1" 'NR==1,NR==var {print $0}' | while read t f; do d=$(date -d @$t "+%b %d %T %Y"); echo "$d -- $f"; done

对于这两个命令,如果您想列出多个文件,还可以更改var="1"参数。

其他回答

ls -Art | tail -n 1

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

使用国旗:

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

-r在排序时倒序

-t按时间排序,latest在前

我也需要这样做,我找到了这些命令。这些对我来说很有用:

如果你想要最后一个文件的创建日期在文件夹(访问时间):

ls -Aru | tail -n 1  

如果你想要最后一个文件的内容有变化(修改时间):

ls -Art | tail -n 1  

我使用:

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

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

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

如果你想获得最近修改的文件,也包括任何子目录,你可以用这个小的联机程序:

find . -type f -exec stat -c '%Y %n' {} \; | sort -nr | awk -v var="1" 'NR==1,NR==var {print $0}' | while read t f; do d=$(date -d @$t "+%b %d %T %Y"); echo "$d -- $f"; done

如果你想对已更改的文件做同样的事情,但对于已访问的文件,你只需更改

参数%Y从stat命令到%X。对于最近访问的文件,你的命令是这样的:

find . -type f -exec stat -c '%X %n' {} \; | sort -nr | awk -v var="1" 'NR==1,NR==var {print $0}' | while read t f; do d=$(date -d @$t "+%b %d %T %Y"); echo "$d -- $f"; done

对于这两个命令,如果您想列出多个文件,还可以更改var="1"参数。