所以适应this SO answer有点非常接近解决方案:
ln -s target-directory/`ls -rt target-directory | grep .log | tail -n1` latest
但我怎么能真正持续更新目录中出现新文件时的符号链接(symbolic link)?
可以使用
inotifywait
存档吗? ?我怎么能在我的系统上安装这样一个在后台处理的作业?
最佳答案
注意解析 ls
输出可能容易出错。见 bash FAQ 99 .
如果 inotifywait
工具可用,您可以执行诸如更新符号链接(symbolic link)之类的操作。
#!/bin/bash
function newest_log
{
files=(*.log)
newest=${files[0]}
for f in "${files[@]}"; do
if [[ $f -nt $newest ]]; then
newest=$f
fi
done
echo $newest
}
while inotifywait -e modify target-directory; do
ln -s target-directory/$(newest_log) latest
done
您可以直接运行此脚本,也可以设置诸如 systemd 服务之类的服务。
关于linux - 根据文件名上的 grep 获取到最新文件的自动更新符号链接(symbolic link),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61162244/