Linux - 借助 inotifywait,轻松实现 Linux 文件/目录事件监听

news2025/2/28 21:01:22

文章目录

  • inotify-tools 依赖包
  • 使用
  • 示例

在这里插入图片描述


inotify-tools 依赖包


[root@VM-24-3-centos ~]# yum install inotify-tools
Loaded plugins: fastestmirror, langpacks
Repository epel is listed more than once in the configuration
Determining fastest mirrors
......
......
......
Resolving Dependencies
--> Running transaction check
---> Package inotify-tools.x86_64 0:3.14-9.el7 will be installed
--> Finished Dependency Resolution

Dependencies Resolved

=======================================================================================================================================
 Package                             Arch                         Version                             Repository                  Size
=======================================================================================================================================
Installing:
 inotify-tools                       x86_64                       3.14-9.el7                          epel                        51 k

Transaction Summary
=======================================================================================================================================
Install  1 Package

Total download size: 51 k
Installed size: 111 k
Is this ok [y/d/N]: y
Downloading packages:
inotify-tools-3.14-9.el7.x86_64.rpm                                                                             |  51 kB  00:00:00
Running transaction check
Running transaction test
Transaction test succeeded
Running transaction
  Installing : inotify-tools-3.14-9.el7.x86_64                                                                                     1/1
  Verifying  : inotify-tools-3.14-9.el7.x86_64                                                                                     1/1

Installed:
  inotify-tools.x86_64 0:3.14-9.el7

Complete!
[root@VM-24-3-centos ~]#


使用


[root@VM-24-3-centos ~]# inotifywait  --help
inotifywait 3.14
Wait for a particular event on a file or set of files.
Usage: inotifywait [ options ] file1 [ file2 ] [ file3 ] [ ... ]
Options:
        -h|--help       Show this help text.
        @<file>         Exclude the specified file from being watched.
        --exclude <pattern>
                        Exclude all events on files matching the
                        extended regular expression <pattern>.
        --excludei <pattern>
                        Like --exclude but case insensitive.
        -m|--monitor    Keep listening for events forever.  Without
                        this option, inotifywait will exit after one
                        event is received.
        -d|--daemon     Same as --monitor, except run in the background
                        logging events to a file specified by --outfile.
                        Implies --syslog.
        -r|--recursive  Watch directories recursively.
        --fromfile <file>
                        Read files to watch from <file> or `-' for stdin.
        -o|--outfile <file>
                        Print events to <file> rather than stdout.
        -s|--syslog     Send errors to syslog rather than stderr.
        -q|--quiet      Print less (only print events).
        -qq             Print nothing (not even events).
        --format <fmt>  Print using a specified printf-like format
                        string; read the man page for more details.
        --timefmt <fmt> strftime-compatible format string for use with
                        %T in --format string.
        -c|--csv        Print events in CSV format.
        -t|--timeout <seconds>
                        When listening for a single event, time out after
                        waiting for an event for <seconds> seconds.
                        If <seconds> is 0, inotifywait will never time out.
        -e|--event <event1> [ -e|--event <event2> ... ]
                Listen for specific event(s).  If omitted, all events are
                listened for.

Exit status:
        0  -  An event you asked to watch for was received.
        1  -  An event you did not ask to watch for was received
              (usually delete_self or unmount), or some error occurred.
        2  -  The --timeout option was given and no events occurred
              in the specified interval of time.

Events:
        access          file or directory contents were read
        modify          file or directory contents were written
        attrib          file or directory attributes changed
        close_write     file or directory closed, after being opened in
                        writeable mode
        close_nowrite   file or directory closed, after being opened in
                        read-only mode
        close           file or directory closed, regardless of read/write mode
        open            file or directory opened
        moved_to        file or directory moved to watched directory
        moved_from      file or directory moved from watched directory
        move            file or directory moved to or from watched directory
        create          file or directory created within watched directory
        delete          file or directory deleted within watched directory
        delete_self     file or directory was deleted
        unmount         file system containing file or directory unmounted
[root@VM-24-3-centos ~]#

常用选项包括:

  • -m​:以持续监视模式运行,即持续监视文件并输出事件。
  • ​-r​:递归监视指定目录及其子目录中的文件。
  • ​-e <event>​:指定要监视的特定事件类型。可以使用多个 -e​ 选项来指定多个事件类型。
  • ​-q​:静默模式,只输出事件信息。
  • ​-s <seconds>​:设置事件之间的最小时间间隔。

使用 inotifywait 命令时,它会持续监视指定的文件或目录,并在事件发生时输出相关信息。可以根据需要处理输出,例如执行其他命令或触发脚本。


示例

监视单个文件的事件:


[root@VM-24-3-centos ~]# inotifywait -e modify -e create a1.txt
Setting up watches.
Watches established.




以上命令将监视 myfile.txt​ 文件的修改和创建事件。

监视单个目录的事件:


[root@VM-24-3-centos ~]# inotifywait -e modify -e create /root
Setting up watches.
Watches established.






以上命令将监视 mydir/​ 目录中文件的修改和创建事件。

监视多个文件或目录的事件:


[root@VM-24-3-centos ~]# touch a1.txt
[root@VM-24-3-centos ~]# touch a2.txt
[root@VM-24-3-centos ~]# inotifywait -e modify -e create a1.txt a2.txt /root
Setting up watches.
Watches established.
^C

以上命令将同时监视 a1.txt​、a2.txt​ 和 mydir/​ 中的文件的修改和创建事件。

如果监视的是目录,则 inotifywait 命令也会观察该目录中的子目录。可以使用 -r​ 选项来递归地监视目录及其子目录中的文件。


#!/bin/bash
# 指定监控目录
directory="/root"
# 监控目录下的文件变更
while inotifywait -r -m  -e create,modify  $directory; do
 # 处理文件变更事件
 echo "File event: $?"
 inotifywait -r  -e create,modify  $directory | while read line; do
   # 输出事件详细信息
   echo "$line"
 done
done

在这里插入图片描述

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.coloradmin.cn/o/896162.html

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!

相关文章

用pytorch实现Resnet

ResNet&#xff08;Residual Network&#xff09;是一种深度卷积神经网络架构&#xff0c;由Kaiming He等人于2015年提出。它在计算机视觉领域引起了革命性的变革&#xff0c;使得训练更深的神经网络成为可能&#xff0c;超越了传统网络架构的限制。 ResNet的主要创新在于…

Matplotlib数据可视化(六)

目录 1.绘制概率图 2.绘制雷达图 3.绘制流向图 4.绘制极坐标图 5.绘制词云图 1.绘制概率图 from scipy.stats import norm fig,ax plt.subplots() plt.rcParams[font.family] [SimHei] np.random.seed() mu 100 sigma 15 x musigma*np.random.randn(437) num_bins …

GUI神器 NXP GUI GUIDER开发工具入门教程

好记性不如烂笔头&#xff0c;既然不够聪明&#xff0c;就乖乖的做笔记&#xff0c;温故而知新。 本文档用于本人对知识点的梳理和记录 目录 一、前言 二、下载 三、演示 四、字体和图片/动画等资源 五、总结 一、前言 GUI Guider是恩智浦为LVGL开发了一个上位机GUI设计工…

初识网络原理(笔记)

目录 ​编辑局域网 网络通信基础 IP 地址 端口号 协议 协议分层 TCP / IP 五层网络模型 网络数据传输的基本流程 发送方的情况&#xff1a; 接收方的情况 局域网 搭建网络的时候&#xff0c;需要用到 交换机 和 路由器 路由器上&#xff0c;有 lan 口 和 wan 口 虽…

涨粉明显,播放暴涨5倍,达人在抖音乘风美拉德

美拉德火了&#xff01; 过去的这个夏天&#xff0c;多巴胺风盛行&#xff0c;无论是街上女孩子们的穿搭&#xff0c;还是店里的陈设&#xff0c;网络上的浏览与讨论&#xff0c;都离不开它。 步入金秋&#xff0c;另一股风刮起。与完美适配夏天五颜六色的多巴胺不同&#xf…

怎么用Python制作一个七夕爱心?

先看效果&#xff1a; 图片效果&#xff1a; 视频效果&#xff1a; 七夕代码效果展示 代码介绍&#xff1a; 这段代码是实现一个用 tkinter 库绘制动态爱心的程序。 用到一些数学函数和常量&#xff0c;例如&#xff1a;sin, cos, pi, log 等等。 定义一个 Heart 类&#x…

msvcp140.dll丢失的解决方法,如何预防msvcp140.dll丢失

在电脑操作系统中经常会弹出类似msvcp140.dll丢失的错误提示窗口&#xff0c;导致软件无法正常运行。为什么会出现msvcp140.dll丢失的情况呢&#xff1f;出现这种情况应该如何解决呢&#xff1f;小编有三种解决方法分享给大家。 一.msvcp140.dll丢失的原因 1.安装过程中受损:在…

Linux下Docker安装及卸载

文章目录 Linux下Docker安装及卸载1 Docker安装及卸载1.1 安装前准备1.2 安装docker软件2.4.3 启动docker2.2.4 测试2.2.5 卸载 Linux下Docker安装及卸载 1 Docker安装及卸载 官方网址&#xff1a;https://docs.docker.com/engine/install/centos/ 1.1 安装前准备 确定你是C…

fedora

about firewall well, I don’t know how to use fedora firewall,but it I come up with that I can use ubuntu’s ufw for blocking evil connection, download wps extremely boredness drove me to install fedora&#xff0c; It can use wps pdf&#xff0c;但我仍然…

学校信息管理系统说明文档

目录 0学生信息管理系统体验教程. 4 0.0Student management异地打开方法&#xff1a;. 4 1. 管理系统设计需求分析. 6 1.1 需求介绍. 6 1.2功能需求. 6 1.2.1 学生信息录入. 6 1.2.2 学生信息查询. 6 1.2.3 权限管理. 6 1.2.4 添加学生信息验证. 6 2.功能介绍. 7 2.1…

LeetCode[491]递增子序列

难度&#xff1a;Medium 题目&#xff1a; 给你一个整数数组 nums &#xff0c;找出并返回所有该数组中不同的递增子序列&#xff0c;递增子序列中 至少有两个元素 。你可以按 任意顺序 返回答案。 数组中可能含有重复元素&#xff0c;如出现两个整数相等&#xff0c;也可以视…

01 小样本学习 概述

一、前言 小样本学习&#xff08;Few-Shot Learning&#xff09;是近几年兴起的一个研究领域&#xff0c;小样本学习旨在解决在数据有限的机器学习任务[参考]。 1.1 小样本学习存在的意义&#xff1f; 近些年&#xff0c;以深度卷积神经网络为代表的深度学习方法在各类机器学…

策略梯度方法

策略梯度方法 数学背景 给定一个标量函数 J ( θ ) J\left(\theta\right) J(θ)&#xff0c;利用梯度上升法&#xff0c;使其最大化&#xff0c;此时的 π θ \pi_\theta πθ​就是最优策略。 θ t 1 θ t α ∇ θ J ( θ t ) \theta_{t1}\theta_t\alpha \nabla_\theta…

C++并发及互斥保护示例

最近要写一个多线程的并发数据库&#xff0c;主要是希望使用读写锁实现库的并发访问&#xff0c;同时考虑到其他平台(如Iar)没有C的读写锁&#xff0c;需要操作系统提供&#xff0c;就将读写锁封装起来。整个过程还是比较曲折的&#xff0c;碰到了不少问题&#xff0c;在此就简…

iOS中一种超简单的路由实现方式

看了很多的路由实现方式&#xff0c;发现实现的都太复杂&#xff0c;越是复杂的东西越是难以维护&#xff0c;当然复杂的东西好处就是覆盖面比较全。而本文则是使用一种比较简单的方式实现路由的&#xff0c;当然如有建议或者想法可以及时跟我沟通&#xff0c;让我也能有更好的…

reeds_sheep运动规划算法Python源码分析

本文用于记录Python版本zhm-real / PathPlanning运动规划库中reeds_sheep算法的源码分析 关于reeds sheep算法的原理介绍前文已经介绍过了&#xff0c;链接如下所示&#xff1a; 《Reeds-Shepp曲线学习笔记及相关思考》 《Reeds-Shepp曲线基础运动公式推导过程》 正文&#xff…

vite初始化vue3项目(配置自动格式化工具与git提交规范工具)

初始化项目 vite构建vue项目还是比较简单的&#xff0c;简单配置选择一下就行了 初始化命令 npm init vuelatest初始化最新版本vue项目 2. 基本选项含义 Add TypeScript 是否添加TSADD JSX是否支持JSXADD Vue Router是否添加Vue Router路由管理工具ADD Pinia 是否添加pinia…

直方图均衡化和自适应直方图均衡化

前言&#xff1a; Hello大家好&#xff0c;我是Dream。 均衡化是数字图像处理中常用的一种技术&#xff0c;用于增强图像的视觉效果和对比度。&#xff0c;今天我们将实现对同一张图像的直方图均衡化和自适应直方图均衡化处理&#xff0c;学习一下两者的的基本原理和实现过程&a…

docker搭建redis主从复制

docker安装redis docker pull redis:latest创建目录结构 用于挂载数据卷配置文件等 运行redis容器 主redis配置 下面这种方式挂载的配置文件不生效。 docker run -d \--name master_redis_6379 \-p 6379:6379 \--restart unless-stopped \-v /mydata/master_redis/data:/da…

集简云本周新增/更新:新增3大功能,集成19款应用,更新5款应用,新增近290个动作

本周更新概要 功能更新 ◉ 新增功能&#xff1a;语聚AI开放API功能 ◉ 新增功能&#xff1a;数据表表格公开分享功能 ◉ 新增功能&#xff1a;浏览器页面操作页面内容读取(增强版本&#xff09; 应用新增 新增应用&#xff1a;赛捷CRM 新增应用&#xff1a;快跑者 新增应…