Linux:目录中的最新文件。

时间:2021-11-13 21:02:08

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

查找将返回目录中最近文件的命令。

Not seeing a limit parameter to ls...

没有看到ls的极限参数……

18 个解决方案

#1


181  

ls -Art | tail -n 1

Not very elegant, but it works.

不是很优雅,但是很有效。

#2


95  

ls -t | head -n1

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

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

#3


41  

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 2 as suggested by Kevin

编辑:使用- f2 -而不是Kevin建议的- f2

#4


9  

I use:

我使用:

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

ls -ABrt1——第一组|尾部-n1

It gives me just the file name, excluding folders.

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

#5


7  

ls -lAtr | tail -1

l -lAtr |尾部-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:

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

ls -latr | tail -1

l -latr | tail -1。

#6


5  

Shorted variant based on dmckee's answer:

基于dmckee的回答缩短了变体:

ls -t | head -1

#7


3  

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

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

#8


2  

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

我个人更喜欢尽可能少地使用不内置的bash命令(以减少昂贵的fork和exec syscalls的数量)。要按日期排序,需要调用ls。但是头部的使用并不是必须的。我使用以下一行代码(只适用于支持名称管道的系统):

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.

我希望这能有所帮助。

#9


2  

The find / sort solution works great until the number of files gets really large (like an entire file system). Use awk instead to just keep track of the most recent file:

查找/排序解决方案非常有效,直到文件的数量变得非常大(比如整个文件系统)。使用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]* //'

#10


1  

ls -t -1 | sed '1q'

Will show the last modified item in the folder. Pair with grep to find latest entries with keywords

将显示文件夹中最后修改过的项。与grep一起查找具有关键字的最新条目

ls -t -1 | grep foo | sed '1q'

#11


1  

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/*/

#12


1  

A note about reliability:

一个关于可靠性的注意事项:

Since the newline character is as valid as any in a file name, any solution that relies on lines like the head/tail based ones are flawed.

由于换行字符与文件名中的任何字符一样有效,因此任何依赖于诸如基于head/tail之类的行的解决方案都是有缺陷的。

With GNU ls, another option is to use the --quoting-style=shell-always option and a bash array:

对于GNU ls,另一个选项是使用—quoting-style=shell-always选项和bash数组:

eval "files=($(ls -t --quoting-style=shell-always))"
((${#files[@]} > 0)) && printf '%s\n' "${files[0]}"

(add -A if you also want to consider hidden files).

(如果您还想考虑隐藏文件,请添加-A)。

If you want to limit to regular files (disregard directories, fifos, devices, symlinks, sockets...), you'd need to resort to GNU find.

如果您想限制常规文件(不考虑目录、fifos、设备、符号链接、套接字…),您需要求助于GNU find。

With bash 4.4 or newer (for readarray -d) and GNU coreutils 8.25 or newer (for cut -z):

对于bash 4.4或更新版本(用于readarray -d)和GNU coreutils 8.25或更新版本(用于cut -z):

readarray -t -d '' < <(
  LC_ALL=C find . -maxdepth 1 -type f ! -name '.*' -printf '%T@/%f\0' |
  sort -rzn | cut -zd/ -f2)

((${#files[@]} > 0)) && printf '%s\n' "${files[0]}"

Or recursively:

或递归地:

readarray -t -d '' < <(
  LC_ALL=C find . -name . -o -name '.*' -prune -o -type f -printf '%T@%p\0' |
  sort -rzn | cut -zd/ -f2-)

Best here would be to use zsh and its glob qualifiers instead of bash to avoid all this hassle:

最好是使用zsh和它的glob限定符而不是bash来避免所有这些麻烦:

Newest regular file in the current directory:

当前目录中的最新常规文件:

printf '%s\n' *(.om[1])

Including hidden ones:

包括隐藏的:

printf '%s\n' *(D.om[1])

Second newest:

第二个最新的:

printf '%s\n' *(.om[2])

Check file age after symlink resolution:

检查符号链接解析后的文件年龄:

printf '%s\n' *(-.om[1])

Recursively:

递归地:

printf '%s\n' **/*(.om[1])

Also, with the completion system (compinit and co) enabled, Ctrl+Xm becomes a completer that expands to the newest file.

此外,启用了完成系统(compinit和co)后,Ctrl+Xm将成为扩展到最新文件的一个完整程序。

So:

所以:

vi Ctrl+Xm

Would make you edit the newest file (you also get a chance to see which it before you press Return).

将使您编辑最新的文件(您还可以在按Return之前查看它)。

vi Alt+2Ctrl+Xm

For the second-newest file.

second-newest文件。

vi *.cCtrl+Xm

for the newest c file.

对于最新的c文件。

vi *(.)Ctrl+Xm

for the newest regular file (not directory, nor fifo/device...), and so on.

对于最新的常规文件(不是目录,也不是fifo/device…),等等。

#13


1  

Recursively:

递归地:

find $1 -type f -exec stat --format '%Y :%y %n' "{}" \; | sort -nr | cut -d: -f2- | head

#14


0  

All those ls/tail solutions work perfectly fine for files in a directory - 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:

按时间排序,见人发现:

%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 %T with %C to sort by ctime.

所以用%T替换%C来排序ctime。

#15


0  

Finding the most current file in every directory according to a pattern, e.g. the sub directories of the working directory that have name ending with "tmp" (case insensitive):

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

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

#16


0  

Presuming you don't care about hidden files that start with a .

假设你不关心以a开头的隐藏文件。

ls -rt | tail -n 1

Otherwise

否则

ls -Art | tail -n 1

#17


0  

using R recursive option .. you may consider this as enhancement for good answers here

使用R递归选项。你可以认为这是对好的答案的增强

ls -arRtlh | tail -50

#18


-1  

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  

#1


181  

ls -Art | tail -n 1

Not very elegant, but it works.

不是很优雅,但是很有效。

#2


95  

ls -t | head -n1

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

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

#3


41  

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 2 as suggested by Kevin

编辑:使用- f2 -而不是Kevin建议的- f2

#4


9  

I use:

我使用:

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

ls -ABrt1——第一组|尾部-n1

It gives me just the file name, excluding folders.

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

#5


7  

ls -lAtr | tail -1

l -lAtr |尾部-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:

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

ls -latr | tail -1

l -latr | tail -1。

#6


5  

Shorted variant based on dmckee's answer:

基于dmckee的回答缩短了变体:

ls -t | head -1

#7


3  

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

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

#8


2  

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

我个人更喜欢尽可能少地使用不内置的bash命令(以减少昂贵的fork和exec syscalls的数量)。要按日期排序,需要调用ls。但是头部的使用并不是必须的。我使用以下一行代码(只适用于支持名称管道的系统):

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.

我希望这能有所帮助。

#9


2  

The find / sort solution works great until the number of files gets really large (like an entire file system). Use awk instead to just keep track of the most recent file:

查找/排序解决方案非常有效,直到文件的数量变得非常大(比如整个文件系统)。使用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]* //'

#10


1  

ls -t -1 | sed '1q'

Will show the last modified item in the folder. Pair with grep to find latest entries with keywords

将显示文件夹中最后修改过的项。与grep一起查找具有关键字的最新条目

ls -t -1 | grep foo | sed '1q'

#11


1  

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/*/

#12


1  

A note about reliability:

一个关于可靠性的注意事项:

Since the newline character is as valid as any in a file name, any solution that relies on lines like the head/tail based ones are flawed.

由于换行字符与文件名中的任何字符一样有效,因此任何依赖于诸如基于head/tail之类的行的解决方案都是有缺陷的。

With GNU ls, another option is to use the --quoting-style=shell-always option and a bash array:

对于GNU ls,另一个选项是使用—quoting-style=shell-always选项和bash数组:

eval "files=($(ls -t --quoting-style=shell-always))"
((${#files[@]} > 0)) && printf '%s\n' "${files[0]}"

(add -A if you also want to consider hidden files).

(如果您还想考虑隐藏文件,请添加-A)。

If you want to limit to regular files (disregard directories, fifos, devices, symlinks, sockets...), you'd need to resort to GNU find.

如果您想限制常规文件(不考虑目录、fifos、设备、符号链接、套接字…),您需要求助于GNU find。

With bash 4.4 or newer (for readarray -d) and GNU coreutils 8.25 or newer (for cut -z):

对于bash 4.4或更新版本(用于readarray -d)和GNU coreutils 8.25或更新版本(用于cut -z):

readarray -t -d '' < <(
  LC_ALL=C find . -maxdepth 1 -type f ! -name '.*' -printf '%T@/%f\0' |
  sort -rzn | cut -zd/ -f2)

((${#files[@]} > 0)) && printf '%s\n' "${files[0]}"

Or recursively:

或递归地:

readarray -t -d '' < <(
  LC_ALL=C find . -name . -o -name '.*' -prune -o -type f -printf '%T@%p\0' |
  sort -rzn | cut -zd/ -f2-)

Best here would be to use zsh and its glob qualifiers instead of bash to avoid all this hassle:

最好是使用zsh和它的glob限定符而不是bash来避免所有这些麻烦:

Newest regular file in the current directory:

当前目录中的最新常规文件:

printf '%s\n' *(.om[1])

Including hidden ones:

包括隐藏的:

printf '%s\n' *(D.om[1])

Second newest:

第二个最新的:

printf '%s\n' *(.om[2])

Check file age after symlink resolution:

检查符号链接解析后的文件年龄:

printf '%s\n' *(-.om[1])

Recursively:

递归地:

printf '%s\n' **/*(.om[1])

Also, with the completion system (compinit and co) enabled, Ctrl+Xm becomes a completer that expands to the newest file.

此外,启用了完成系统(compinit和co)后,Ctrl+Xm将成为扩展到最新文件的一个完整程序。

So:

所以:

vi Ctrl+Xm

Would make you edit the newest file (you also get a chance to see which it before you press Return).

将使您编辑最新的文件(您还可以在按Return之前查看它)。

vi Alt+2Ctrl+Xm

For the second-newest file.

second-newest文件。

vi *.cCtrl+Xm

for the newest c file.

对于最新的c文件。

vi *(.)Ctrl+Xm

for the newest regular file (not directory, nor fifo/device...), and so on.

对于最新的常规文件(不是目录,也不是fifo/device…),等等。

#13


1  

Recursively:

递归地:

find $1 -type f -exec stat --format '%Y :%y %n' "{}" \; | sort -nr | cut -d: -f2- | head

#14


0  

All those ls/tail solutions work perfectly fine for files in a directory - 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:

按时间排序,见人发现:

%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 %T with %C to sort by ctime.

所以用%T替换%C来排序ctime。

#15


0  

Finding the most current file in every directory according to a pattern, e.g. the sub directories of the working directory that have name ending with "tmp" (case insensitive):

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

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

#16


0  

Presuming you don't care about hidden files that start with a .

假设你不关心以a开头的隐藏文件。

ls -rt | tail -n 1

Otherwise

否则

ls -Art | tail -n 1

#17


0  

using R recursive option .. you may consider this as enhancement for good answers here

使用R递归选项。你可以认为这是对好的答案的增强

ls -arRtlh | tail -50

#18


-1  

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