所以稍微适应this SO answer非常接近解决方案:
ln -s target-directory/`ls -rt target-directory | grep .log | tail -n1` latest
但是当目录中出现新文件时,我怎么实际上连续更新符号链接?
可以使用inotifywait
将其存档吗?我该如何在我的系统上安装这样的作业,并在后台进行维护?
请注意,解析ls
输出可能容易出错。参见bash FAQ 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服务。