[英]Get most recent file in a directory on Linux
寻找将返回目录中单个最新文件的命令。
没有看到ls
的限制参数...
ls -Art | tail -n 1
不是很优雅,但它有效。
ls -t | head -n1
该命令实际上给出了当前工作目录中最新修改的文件。
这是一个递归版本(即它在某个目录或其任何子目录中找到最近更新的文件)
find $DIR -type f -printf "%T@ %p\n" | sort -n | cut -d' ' -f 2- | tail -n 1
编辑:按照凯文的建议使用-f 2-
而不是-f 2
关于可靠性的说明:
由于换行符与文件名中的任何字符一样有效,因此任何依赖于基于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
)。
如果您想限制为常规文件(忽略目录、fifos、设备、符号链接、套接字...),您需要求助于 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
和它的 glob 限定符而不是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
和 co)后, Ctrl+X m成为扩展到最新文件的完成器。
所以:
vi Ctrl+Xm
会让你编辑最新的文件(你也有机会在按下Return之前看到它)。
vi Alt+2Ctrl+Xm
对于第二个最新的文件。
vi *.cCtrl+Xm
对于最新的c
文件。
vi *(.)Ctrl+Xm
对于最新的常规文件(不是目录,也不是 fifo/device...),依此类推。
我用:
ls -ABrt1 --group-directories-first | tail -n1
它只给我文件名,不包括文件夹。
ls -lAtr | tail -1
其他解决方案不包括以'.'
开头的文件'.'
.
此命令还将包含'.'
和'..'
,这可能是也可能不是你想要的:
ls -latr | tail -1
查找/排序解决方案非常有效,直到文件数量变得非常大(就像整个文件系统一样)。 改用 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]* //'
基于 dmckee 的回答的短路变体:
ls -t | head -1
我喜欢echo *(om[1])
( zsh
语法),因为zsh
给出文件名而不调用任何其他命令。
如果您想获取最新更改的文件,还包括任何子目录,您可以使用这个小小的 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
命令(以减少昂贵的 fork 和 exec 系统调用的数量)。 要按日期排序,需要调用ls
。 但是使用head
并不是真正必要的。 我使用以下单行(仅适用于支持名称管道的系统):
read newest < <(ls -t *.log)
或获取最旧文件的名称
read oldest < <(ls -rt *.log)
(注意两个 '<' 标记之间的空格!)
如果还需要隐藏文件 - 可以添加 arg。
我希望这会有所帮助。
使用 R 递归选项 .. 您可以将其视为此处的好答案的增强
ls -arRtlh | tail -50
只有 Bash 内置函数,紧跟BashFAQ/003 :
shopt -s nullglob
for f in * .*; do
[[ -d $f ]] && continue
[[ $f -nt $latest ]] && latest=$f
done
printf '%s\n' "$latest"
试试这个简单的命令
ls -ltq <path> | head -n 1
如果你想要文件名 - 最后修改,路径 = /ab/cd/*.log
如果你想要目录名 - 最后修改,路径 = /ab/cd/*/
ls -t -1 | sed '1q'
将显示文件夹中最后修改的项目。 与grep
配对以查找带有关键字的最新条目
ls -t -1 | grep foo | sed '1q'
递归地:
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 -Frt | grep "[^/]$" | tail -n 1
所有这些 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 "{}"
这按 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.
所以只需用%C
替换%T
即可按 ctime 排序。
根据模式查找每个目录中的最新文件,例如工作目录的子目录名称以“tmp”结尾(不区分大小写):
find . -iname \*tmp -type d -exec sh -c "ls -lArt {} | tail -n 1" \;
查找前缀为shravan*
的文件并查看
less $(ls -Art shravan* | tail -n 1)
如果你想在 /apps/test 目录中找到最后修改的文件夹名称,那么你可以将下面的代码片段放在批处理脚本中并执行它,这将打印最后修改的文件夹名称的名称。
#!/bin/bash -e
export latestModifiedFolderName=$(ls -td /apps/test/*/ | head -n1)
echo Latest modified folder within /apps/test directory is $latestModifiedFolderName
我也需要这样做,我找到了这些命令。 这些对我有用:
如果您希望按文件夹中的创建日期(访问时间)显示最后一个文件:
ls -Aru | tail -n 1
如果您想要内容发生变化的最后一个文件(修改时间):
ls -Art | tail -n 1
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.