码迷,mamicode.com
首页 > 系统相关 > 详细

linux怎么实时查看目录下是否有文件生成

时间:2018-11-06 18:15:15      阅读:343      评论:0      收藏:0      [点我收藏+]

标签:记录   日志   span   lin   install   color   touch   inux   run   

安装inotify-tools

$ sudo apt install inotify-tools -y

 

创建数据和日志目录

$ sudo mkdir -p /tmp/inotifywait/log/ /tmp/inotifywait/data/

 

创建日志文件

$ touch /tmp/inotifywait/log/data.log

假设在 /tmp/inotifywait/data/ 中不断生成(删除,移动)数据,/tmp/inotifywait/log/data.log 文件中记录日志

 

开启监控 (这里简称“会话1”)

$ inotifywait -mrq --timefmt %y-%m-%d %H:%M --format %T %e %w%f -e create -e delete -e move -e moved_to -e moved_from -e delete_self  /tmp/inotifywait/data/

再开一会话终端(这里简称“会话2”),并切换到到 /tmp/inotifywait/data/ 目录,我们在里面使用 Crunch 创建一些字典文件(或者你在里面随便创建,移动,删除些什么),不清楚 Crunch 是什么,怎么用的请参考《crunch--字典生成工具

$ cd /tmp/inotifywait/data/
$ crunch 5 5 -b 20mib -o START

 

这时,在会话1中会输出

18-11-06 17:06 CREATE /tmp/inotifywait/data/START
18-11-06 17:06 MOVED_FROM /tmp/inotifywait/data/START
18-11-06 17:06 MOVED_TO /tmp/inotifywait/data/aaaaa-hqwmu.txt
18-11-06 17:06 CREATE /tmp/inotifywait/data/START
18-11-06 17:06 MOVED_FROM /tmp/inotifywait/data/START
18-11-06 17:06 MOVED_TO /tmp/inotifywait/data/hqwmv-phszp.txt
18-11-06 17:06 CREATE /tmp/inotifywait/data/START
18-11-06 17:06 MOVED_FROM /tmp/inotifywait/data/START
18-11-06 17:06 MOVED_TO /tmp/inotifywait/data/phszq-wypmk.txt
18-11-06 17:06 CREATE /tmp/inotifywait/data/START
18-11-06 17:06 MOVED_FROM /tmp/inotifywait/data/START
18-11-06 17:06 MOVED_TO /tmp/inotifywait/data/wypml-zzzzz.txt

 

后台运行

Ctrl+C 在会话1中退出notifywait,采用后台运行的方式记录日志

# 监控后台运行并输出到文件

$ inotifywait -mrqd --timefmt %y-%m-%d %H:%M --format %T %e %w%f -e create -e delete -e move -e moved_to -e moved_from -e delete_self -o /tmp/inotifywait/log/data.log  /tmp/inotifywait/data/

删除/tmp/inotifywait/data/目录下的所有文件,重新生成字典文件(过程略)

$ cat  /tmp/inotifywait/log/data.log

18-11-06 17:34 DELETE /tmp/inotifywait/data/aaaaa-hqwmu.txt
18-11-06 17:34 DELETE /tmp/inotifywait/data/hqwmv-phszp.txt
18-11-06 17:34 DELETE /tmp/inotifywait/data/phszq-wypmk.txt
18-11-06 17:34 DELETE /tmp/inotifywait/data/wypml-zzzzz.txt
18-11-06 17:34 CREATE /tmp/inotifywait/data/START
18-11-06 17:34 MOVED_FROM /tmp/inotifywait/data/START
18-11-06 17:34 MOVED_TO /tmp/inotifywait/data/aaaaa-hqwmu.txt
18-11-06 17:34 CREATE /tmp/inotifywait/data/START
18-11-06 17:34 MOVED_FROM /tmp/inotifywait/data/START
18-11-06 17:34 MOVED_TO /tmp/inotifywait/data/hqwmv-phszp.txt
18-11-06 17:34 CREATE /tmp/inotifywait/data/START
18-11-06 17:34 MOVED_FROM /tmp/inotifywait/data/START
18-11-06 17:34 MOVED_TO /tmp/inotifywait/data/phszq-wypmk.txt
18-11-06 17:34 CREATE /tmp/inotifywait/data/START
18-11-06 17:34 MOVED_FROM /tmp/inotifywait/data/START
18-11-06 17:34 MOVED_TO /tmp/inotifywait/data/wypml-zzzzz.txt

 

linux怎么实时查看目录下是否有文件生成

标签:记录   日志   span   lin   install   color   touch   inux   run   

原文地址:https://www.cnblogs.com/wpjamer/p/9916842.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!