因此,稍微调整一下这个 SO 答案非常接近解决方案:
ln -s target-directory/`ls -rt target-directory | grep .log | tail -n1` latest
但是当目录中出现新文件时,我如何才能真正持续更新符号链接?
这可以通过使用存档inotifywait
吗?我怎么能在我的系统上安装这样一个在后台处理的作业?
因此,稍微调整一下这个 SO 答案非常接近解决方案:
ln -s target-directory/`ls -rt target-directory | grep .log | tail -n1` latest
但是当目录中出现新文件时,我如何才能真正持续更新符号链接?
这可以通过使用存档inotifywait
吗?我怎么能在我的系统上安装这样一个在后台处理的作业?
请注意,解析ls
输出可能容易出错。请参阅bash 常见问题解答 99。
如果inotifywait
工具可用,您可以执行诸如更新符号链接之类的操作。
#!/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服务之类的服务。