寻找返回目录中最新文件的命令。
没有看到ls的limit参数…
寻找返回目录中最新文件的命令。
没有看到ls的limit参数…
当前回答
只有Bash内置程序,紧跟BashFAQ/003:
shopt -s nullglob
for f in * .*; do
[[ -d $f ]] && continue
[[ $f -nt $latest ]] && latest=$f
done
printf '%s\n' "$latest"
其他回答
只有Bash内置程序,紧跟BashFAQ/003:
shopt -s nullglob
for f in * .*; do
[[ -d $f ]] && continue
[[ $f -nt $latest ]] && latest=$f
done
printf '%s\n' "$latest"
ls -t -1 | sed '1q'
将显示文件夹中最后修改的项目。与grep配对以查找带有关键字的最新条目
ls -t -1 | grep foo | sed '1q'
我也需要这样做,我找到了这些命令。这些对我来说很有用:
如果你想要最后一个文件的创建日期在文件夹(访问时间):
ls -Aru | tail -n 1
如果你想要最后一个文件的内容有变化(修改时间):
ls -Art | tail -n 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]* //'
根据模式在每个目录中查找最新的文件,例如工作目录的子目录的名称以“tmp”结尾(不区分大小写):
find . -iname \*tmp -type d -exec sh -c "ls -lArt {} | tail -n 1" \;