Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/linux/25.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在Linux上获取目录中的最新文件_Linux_Shell_Command Line - Fatal编程技术网

在Linux上获取目录中的最新文件

在Linux上获取目录中的最新文件,linux,shell,command-line,Linux,Shell,Command Line,查找将返回目录中单个最新文件的命令 没有看到限制参数到ls ls -Art | tail -n 1 不是很优雅,但很管用 ls -t | head -n1 这个命令实际上给出了当前工作目录中最新修改的文件 ls-lAtr | tail-1 其他解决方案不包括以开头的文件。 此命令还将包括'.和'...,这可能是您想要的,也可能不是您想要的: ls-latr | tail-1我使用: ls-ABrt1——首先分组目录| tail-n1 它只给出文件名,不包括文件夹。我喜欢echo*(om[1]

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

没有看到限制参数到
ls

ls -Art | tail -n 1
不是很优雅,但很管用

ls -t | head -n1

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

ls-lAtr | tail-1

其他解决方案不包括以
开头的文件。

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

ls-latr | tail-1

我使用:

ls-ABrt1——首先分组目录| tail-n1


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

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

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


编辑:使用
-f2-
代替凯文建议的
-f2
,试试这个简单的命令

ls -ltq  <path>  | head -n 1
ls-ltq |头-n 1
如果您想要文件名-上次修改,路径=/ab/cd/*.log


如果您想要目录名-last modified,path=/ab/cd/*/

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


阅读最新的<我也需要这样做,我发现了这些命令。这些对我来说很有用:

如果要在文件夹中创建最后一个文件(访问时间),请执行以下操作:

如果希望最后一个文件的内容发生更改(修改时间):


所有这些ls/tail解决方案对于目录中的文件都能很好地工作——忽略子目录

为了在搜索中包含所有文件(递归),可以使用find。Giole建议对格式化的find输出进行排序。但是要注意空格(他的建议对空格不起作用)

这应适用于所有文件名:

find $DIR -type f -printf "%T@ %p\n" | sort -n | sed -r 's/^[0-9.]+\s+//' | tail -n 1 | xargs -I{} ls -l "{}"
这将按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.
因此,只需将
%T
替换为
%C
即可按ctime排序

ls -t -1 | sed '1q'
将显示文件夹中最后修改的项目。与
grep
配对以查找包含关键字的最新条目

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

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

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


关于可靠性的说明:

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

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

eval "files=($(ls -t --quoting-style=shell-always))"
((${#files[@]} > 0)) && printf '%s\n' "${files[0]}"
< P>(添加代码< >代码> >选项> <代码> ls >代码>如果您还想考虑隐藏文件) 如果您想限制为常规文件(忽略目录、FIFO、设备、符号链接、套接字…),则需要求助于GNU
find

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

包括隐藏的:

printf '%s\n' *(D.om[1])
第二最新版本:

printf '%s\n' *(.om[2])
在符号链接解析后检查文件期限:

printf '%s\n' *(-.om[1])
递归地:

readarray -t -d '' files < <(
  LC_ALL=C find . -name . -o -name '.*' -prune -o -type f -printf '%T@%p\0' |
  sort -rzn | cut -zd/ -f2-)
printf '%s\n' **/*(.om[1])
find $1 -type f -exec stat --format '%Y :%y %n' "{}" \; | sort -nr | cut -d: -f2- | head
此外,在启用了完成系统(
compinit
和co)的情况下,Ctrl+Xm将成为一个扩展到最新文件的完成器

vi *.cCtrl+Xm 因此:


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

根据dmckee的回答缩短变量:

ls -t | head -1
递归地:

readarray -t -d '' files < <(
  LC_ALL=C find . -name . -o -name '.*' -prune -o -type f -printf '%T@%p\0' |
  sort -rzn | cut -zd/ -f2-)
printf '%s\n' **/*(.om[1])
find $1 -type f -exec stat --format '%Y :%y %n' "{}" \; | sort -nr | cut -d: -f2- | head

假设您不关心以
开头的隐藏文件

ls -rt | tail -n 1
否则

ls -Art | tail -n 1

使用R递归选项。。你可以认为这是一个很好的答案。
ls -arRtlh | tail -50

如果您想获取最新更改的文件(包括任何子目录),可以使用以下小oneliner:

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”参数。

仅使用Bash内置,如下所示:

shopt-s nullglob
对于**中的f;做
[[-d$f]]&继续(&C)
[[$f-新台币$latest]]&&latest=$f
完成
printf“%s\n”$latest

相当于我的,而且可能更高效。对我来说不太合适,因为我先在ls中回显一些信息,但我得到了使用方法,谢谢@Josir这可以修改为包含带有
ls-tl | head-n1
的邮戳,并且它不必像我的一样将整个表推过管道。@noɥ647;ԀzɹƆ
ls-t*.png | head-n1
如果它是最新修改的,则返回一个文件夹,使用:
ls-Art | head-n1
如果您特别想要最新修改的文件,此文件是否会返回“.”如果目录的修改时间比包含的任何文件都要晚(比如删除)?也许这是人们想要的行为,也许不是。但不管怎样,你都是对的。
watch-n1'ls-Art | tail-n1'
-显示了最后一个文件这里的大多数答案解析
ls
的输出,或者使用
find
而不使用
-print0
,这对于处理恼人的文件名来说是有问题的。总是有必要提及:它给出了这个问题的POSIX答案,除非目录中只有directoriesusin vi *(.)Ctrl+Xm
ls -t | head -1
find $1 -type f -exec stat --format '%Y :%y %n' "{}" \; | sort -nr | cut -d: -f2- | head
ls -rt | tail -n 1
ls -Art | tail -n 1
ls -arRtlh | tail -50
ls -Frt | grep "[^/]$" | tail -n 1
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
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