0

因此,稍微调整一下这个 SO 答案非常接近解决方案:

ln -s target-directory/`ls -rt target-directory | grep .log | tail -n1` latest

但是当目录中出现新文件时,我如何才能真正持续更新符号链接?

这可以通过使用存档inotifywait吗?我怎么能在我的系统上安装这样一个在后台处理的作业?

4

1 回答 1

1

请注意,解析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服务之类的服务。

于 2020-04-11T19:40:02.870 回答