获取最新的文件在Linux上的目录获取最新的文件在Linux上的目录(Get most recent

2019-05-13 13:02发布

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

没有看到极限参数LS ...

Answer 1:

ls -Art | tail -n 1

不是很优雅,但它的工作原理。



Answer 2:

ls -t | head -n1

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



Answer 3:

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

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

编辑:使用-f 2-代替-f 2由Kevin所建议



Answer 4:

我用:

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

这使我只是文件名,不包括文件夹。



Answer 5:

ls -lAtr | tail -1

其他的解决方案不包括与启动文件'.'

该命令还包括'.''..' ,这可能是也可能不是你想要什么:

ls -latr | tail -1



Answer 6:

基于dmckee的回答短路变种:

ls -t | head -1


Answer 7:

关于可靠性的注意事项:

由于换行符是任何一个文件名,它依赖于像线的任何解决方案,有效的head / tail基础的人都是有缺陷的。

与GNU ls ,另一种选择是使用--quoting-style=shell-always选项和bash数组:

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

(添加-A选项ls ,如果你还需要考虑隐藏文件)。

如果你想限制常规文件(无视目录,FIFO中,设备,符号链接,插座...),你需要求助于GNU find

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

readarray -t -d '' files < <(
  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]}"

或递归:

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

这里最好是使用zsh和水珠预选赛,而不是bash ,以避免这一切的麻烦:

最新常规文件在当前目录:

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

包括隐藏的:

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

第二最新:

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

检查文件的符号链接岁决议日后:

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

递归:

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

此外,在完井系统( compinit启用和共) 键,Ctrl + X 变成扩展为最新的文件完成者。

所以:

vi Ctrl+Xm

会让你编辑的最新文件(你也有机会看到它,你按一下回车键之前)。

vi Alt+2Ctrl+Xm

对于第二最新的文件。

vi *.cCtrl+Xm

对于最新的c文件。

vi *(.)Ctrl+Xm

对于最新的常规文件(没有目录,也不FIFO /设备......),等等。



Answer 8:

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


Answer 9:

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



Answer 10:

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

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

或获得最早的文件名

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

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

如果还需要隐藏的文件可以添加-A ARG。

我希望这可以帮助。



Answer 11:

,使用R递归选项..你可能会认为这是增强了这里良好的答案

ls -arRtlh | tail -50


Answer 12:

ls -t -1 | sed '1q'

将显示该文件夹中的最后一个修改项目。 配对grep找到与关键字最新条目

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


Answer 13:

试试这个简单的命令

ls -ltq  <path>  | head -n 1

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

如果你想要的目录名称 - 最后修改路径= / AB / CD / * /



Answer 14:

递归:

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


Answer 15:

所有这些LS /尾的解决方案工作的文件完全正常目录-忽略子目录。

为了在您的搜索(递归)的所有文件,找到可以使用。 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.

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



Answer 16:

根据图案查找最新的文件中的所有目录,例如工作目录的子目录有名称以“TMP”(不区分大小写)结束:

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


Answer 17:

假设你不关心,与一开始隐藏文件.

ls -rt | tail -n 1

除此以外

ls -Art | tail -n 1


Answer 18:

ls -Frt | grep "[^/]$" | tail -n 1


Answer 19:

我需要做到这一点,我发现这些命令。 这些工作对我来说:

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

ls -Aru | tail -n 1  

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

ls -Art | tail -n 1  


文章来源: Get most recent file in a directory on Linux