在unix系统上,我如何监控一个目录(就像“tail”的工作原理一样)文件的改变--无论是新创建的改变,还是大小的改变,等等。
寻找命令行工具,而不是要安装的东西。
发布于 2010-10-21 00:31:00
大多数unix变体都有用于此的API,但它没有标准化。在Linux上,有inotify。在命令行中,可以使用inotifywait。使用示例:
inotifywait -m /path/to/dir | while read -r dir event name; do
case $event in
OPEN) echo "The file $name was created or opened (not necessarily for writing)";;
WRITE) echo "The file $name was written to";;
DELETE) echo "The file $name was deleted ";;
esac
doneInotify事件类型通常并不是你想要注意到的(例如,OPEN是非常宽的),所以如果你最终自己做了文件检查,也不要感到难受。
发布于 2010-10-20 20:56:49
如果你不想安装工具,你可以自己制作。这只是个想法。使用find命令创建目录的基本行文件。使用循环或cron作业,使用相同的参数对目录执行find操作,并根据基线文件检查新文件。使用像diff这样的工具来获取差异。
例如
find /path [other options] >> baseline.txt
while true #or use a cron job
do
find /path [same options] >> listing.txt
diff baseline.txt listing.txt
# do processing here...
mv listing.txt baseline.txt # update the baseline.
sleep 60
donehttps://stackoverflow.com/questions/3977890
复制相似问题