使用 inotify-tools 监控 Samba 目录中的所有子文件夹更改

在 Linux 系统中,可以使用 inotify-tools 来监控目录中的文件和子文件夹的变化。这对于监控 Samba 共享目录的更改非常有用。

1. 安装 inotify-tools

首先,您需要安装 inotify-tools。可以使用以下命令在 Ubuntu/Debian 系统中安装:

sudo apt-get install inotify-tools

2. 创建监控脚本

创建一个名为 monitor.sh 的文件,并将以下内容复制粘贴到文件中:

#!/bin/bash

DIR_TO_WATCH='/path/to/directory'

inotifywait -m -r -e modify,create,delete $DIR_TO_WATCH | while read path action file; do
    echo 'The file '$file' appeared in directory '$path' via '$action''
    # 这里可以添加你想要执行的命令
done

DIR_TO_WATCH 变量的值更改为要监视的 Samba 共享目录的路径。

3. 设置脚本可执行

使用以下命令将 monitor.sh 脚本设置为可执行:

chmod +x monitor.sh

4. 启动监控脚本

使用以下命令启动 monitor.sh 脚本:

./monitor.sh

现在,当目录中的任何文件或子文件夹被修改、创建或删除时,脚本都会输出相应的消息。您可以在脚本中添加您想要执行的命令来处理这些更改。

示例:

假设您要监控 /mnt/share 目录,您可以将 DIR_TO_WATCH 设置为 /mnt/share,并将以下代码添加到脚本中来记录所有更改到一个名为 changes.log 的文件中:

#!/bin/bash

DIR_TO_WATCH='/mnt/share'

inotifywait -m -r -e modify,create,delete $DIR_TO_WATCH | while read path action file; do
    echo 'The file '$file' appeared in directory '$path' via '$action'' >> changes.log
done

通过这种方式,您可以轻松监控 Samba 共享目录中的所有子文件夹更改。

使用 inotify-tools 监控 Samba 目录中的所有子文件夹更改

原文地址: https://www.cveoy.top/t/topic/ld8m 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录