寻找返回目录中最新文件的命令。
没有看到ls的limit参数…
寻找返回目录中最新文件的命令。
没有看到ls的limit参数…
当前回答
ls -lAtr | tail -1
其他解决方案不包括以“。”开头的文件。
该命令还包括'。'和'..,这可能是也可能不是你想要的:
Ls -latr | tail -1
其他回答
这是一个递归版本(即,它在某个目录或其任何子目录中查找最近更新的文件)
find /dir/path -type f -printf "%T@ %p\n" | sort -n | cut -d' ' -f 2- | tail -n 1
命令行简单的外行解释:
find /dir/path -type f finds all the files in the directory -printf "%T@ %p\n" prints a line for each file where %T@ is the float seconds since 1970 epoch and %p is the filename path and \n is the new line character for more info see man find | is a shell pipe (see man bash section on Pipelines) sort -n means to sort on the first column and to treat the token as numerical instead of lexicographic (see man sort) cut -d' ' -f 2- means to split each line using the character and then to print all tokens starting at the second token (see man cut) NOTE: -f 2 would print only the second token tail -n 1 means to print the last line (see man tail)
我喜欢echo *(om[1]) (zsh语法),因为它只给出文件名,而不调用任何其他命令。
我使用:
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排序。
我个人更喜欢使用尽可能少的非内置bash命令(以减少昂贵的fork和exec系统调用的数量)。要按日期排序,需要调用ls。但使用头部是没有必要的。我使用以下一行代码(只适用于支持名称管道的系统):
read newest < <(ls -t *.log)
或者获取最古老的文件的名称
read oldest < <(ls -rt *.log)
(注意两个“<”符号之间的空格!)
如果还需要隐藏文件,可以添加一个参数。
我希望这能有所帮助。