linux - 根据文件名上的 grep 获取到最新文件的自动更新符号链接(symbolic link)

标签 linux ubuntu unix inotify inotifywait

所以适应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/

相关文章:

python - 什么是使用交换。 python 版本

linux - 在 Ubuntu 16.04 中安装 Vim 时出错

python - Subversion 提交后钩子(Hook)

python - Bash - 标准输出文本在重定向到文件时被截断

linux - cron 的替代品?

php - 如何在 PHP 的 eclipse 内容辅助中禁用 "No Default Proposals"?

ubuntu - 无法使用 pip (ubuntu) [pip 配置有需要 TLS/SSL 的位置,但是 Python 中的 ssl 模块不可用。 ]

bash - 文本处理 - 如何使用 sed 从搜索结果中删除部分字符串?

unix - 如何从一行文本中选择给定的列?

linux - 将之前忽略的目录添加到 Git 存储库