获取 Linux 目录中的最新文件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1015678/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-03 17:26:27  来源:igfitidea点击:

Get most recent file in a directory on Linux

linuxcommand-line

提问by ack

Looking for a command that will return the single most recent file in a directory.

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

Not seeing a limit parameter to ls...

没有看到 ls 的限制参数...

采纳答案by dmckee --- ex-moderator kitten

ls -Art | tail -n 1

Not very elegant, but it works.

不是很优雅,但它有效。

回答by chaos

ls -t | head -n1

This command actually gives the latest modified file in the current working directory.

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

回答by Jared Oberhaus

ls -lAtr | tail -1

ls -lAtr | tail -1

The other solutions do not include files that start with '.'.

其他解决方案不包括以'.'.

This command will also include '.'and '..', which may or may not be what you want:

此命令还将包括'.'and '..',这可能是您想要的,也可能不是:

ls -latr | tail -1

ls -latr | tail -1

回答by user1195354

I use:

我用:

ls -ABrt1 --group-directories-first | tail -n1

ls -ABrt1 --group-directories-first | tail -n1

It gives me just the file name, excluding folders.

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

回答by MikeB

I like echo *(om[1])(zshsyntax) as that just gives the file name and doesn't invoke any other command.

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

回答by gioele

This is a recursive version (i.e. it finds the most recently updated file in a certain directory or any of its subdirectory)

这是一个递归版本(即它在某个目录或其任何子目录中找到最近更新的文件)

find $DIR -type f -printf "%T@ %p\n" | sort -n | cut -d' ' -f 2- | tail -n 1

Edit: use -f 2-instead of -f 2as suggested by Kevin

编辑:使用-f 2-而不是-f 2按照凯文的建议

回答by RICHA AGGARWAL

try this simple command

试试这个简单的命令

ls -ltq  <path>  | head -n 1

If you want file name - last modified, path = /ab/cd/*.log

如果你想要文件名 - 最后修改,路径 = /ab/cd/*.log

If you want directory name - last modified, path = /ab/cd/*/

如果你想要目录名 - 最后修改,路径 = /ab/cd/*/

回答by TrueY

I personally prefer to use as few not built-in bashcommands as I can (to reduce the number of expensive fork and exec syscalls). To sort by date the lsneeded to be called. But using of headis not really necessary. I use the following one-liner (works only on systems supporting name pipes):

我个人更喜欢尽可能少地使用非内置bash命令(以减少昂贵的 fork 和 exec 系统调用的数量)。按ls需要调用的日期排序。但是使用 ofhead并不是真正必要的。我使用以下单行(仅适用于支持名称管道的系统):

read newest < <(ls -t *.log)

or to get the name of the oldest file

或获取最旧文件的名称

read oldest < <(ls -rt *.log)

(Mind the space between the two '<' marks!)

(注意两个“<”标记之间的空格!)

If the hidden files are also needed -A arg could be added.

如果还需要隐藏文件 - 可以添加 arg。

I hope this could help.

我希望这会有所帮助。

回答by masoomeh vahid

I needed to do it too, and I found these commands. these work for me:

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

If you want last file by its date of creation in folder(access time) :

如果您希望按文件夹中的创建日期(访问时间)显示最后一个文件:

ls -Aru | tail -n 1  

And if you want last file that has changes in its content (modify time) :

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

ls -Art | tail -n 1  

回答by basic6

All those ls/tail solutions work perfectly fine for files in adirectory - ignoring subdirectories.

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

In order to include all files in your search (recursively), find can be used. gioele suggested sorting the formatted find output. But be careful with whitespaces (his suggestion doesn't work with whitespaces).

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

This should work with all file names:

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

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

This sorts by mtime, see man find:

这按 mtime 排序,请参阅 man find:

%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.

So just replace %Twith %Cto sort by ctime.

因此,只需更换%T%C通过的ctime排序。