您尚未登录。

楼主 #1 2020-04-08 13:51:55

pythinker
会员
注册时间: 2019-02-12
已发帖子: 215
积分: 215

请问大家Linux shell 脚本如何检测一个指定的文件夹有变化, 包括文件新增,修改,删除等

如题.

离线

#2 2020-04-08 13:54:48

smartcar
会员
注册时间: 2018-02-19
已发帖子: 735
积分: 735

Re: 请问大家Linux shell 脚本如何检测一个指定的文件夹有变化, 包括文件新增,修改,删除等

试一试 inotifywait 命令。

离线

楼主 #3 2020-04-08 13:57:16

pythinker
会员
注册时间: 2019-02-12
已发帖子: 215
积分: 215

Re: 请问大家Linux shell 脚本如何检测一个指定的文件夹有变化, 包括文件新增,修改,删除等

smartcar 说:

试一试 inotifywait 命令。

谢谢啊, 这个不错, 试了一下

https://unix.stackexchange.com/questions/24952/script-to-monitor-folder-for-new-files

$ inotifywait -m /tmp -e create -e moved_to
Setting up watches.
Watches established.
/tmp/ CREATE sh-thd.HxgPGz
/tmp/ CREATE aaaa
/tmp/ CREATE,ISDIR ccc

现在正在找怎么获取新增文件的全路径。

离线

楼主 #4 2020-04-08 14:09:11

pythinker
会员
注册时间: 2019-02-12
已发帖子: 215
积分: 215

Re: 请问大家Linux shell 脚本如何检测一个指定的文件夹有变化, 包括文件新增,修改,删除等

递归监测 /tmp 目录变化:

inotifywait --recursive -m /tmp -e create -e moved_to |
    while read dir action file; do
        echo "The file '$file' appeared in directory '$dir' via '$action'"
        # do something with the file
    done
$ inotifywait --recursive -m /tmp -e create -e moved_to |
>     while read dir action file; do
>         echo "The file '$file' appeared in directory '$dir' via '$action'"
>         # do something with the file
>     done
Setting up watches.  Beware: since -r was given, this may take a while!
Watches established.
The file 'aaaanb' appeared in directory '/tmp/ccc/ddd/ee/ff/g/' via 'CREATE'
The file 'sh-thd.mdd8J1' appeared in directory '/tmp/' via 'CREATE'
The file 'sh-thd.WX0x9Z' appeared in directory '/tmp/' via 'CREATE'
The file '555' appeared in directory '/tmp/ccc/ddd/ee/ff/' via 'CREATE'

离线

页脚

工信部备案:粤ICP备20025096号 Powered by FluxBB

感谢为中文互联网持续输出优质内容的各位老铁们。 QQ: 516333132, 微信(wechat): whycan_cn (哇酷网/挖坑网/填坑网) service@whycan.cn