Linux 如何监视包含所有子文件夹和文件的文件夹?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9067175/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to monitor a folder with all subfolders and files inside?
提问by just ME
I have a folder called "Datas". This folder has a subfolder called "Inbox" inside of which there are multiple ".txt" files. This "Datas" folder can be modified and in the end there will be multiple subfolders with "Inbox" subfolders and ".txt" files. I need to monitor the "Datas" folder and the ".txt" file from the "Inbox" folder. How can I do that?
我有一个名为“Datas”的文件夹。该文件夹有一个名为“收件箱”的子文件夹,其中有多个“.txt”文件。这个“Datas”文件夹可以修改,最后会有多个子文件夹,其中包含“Inbox”子文件夹和“.txt”文件。我需要监视“数据”文件夹和“收件箱”文件夹中的“.txt”文件。我怎样才能做到这一点?
INotify is just monitoring a folder and pops events when subfolders are created. How to pop events when ".txt" files are created (in which folder)?
INotify 只是监视文件夹并在创建子文件夹时弹出事件。创建“.txt”文件时如何弹出事件(在哪个文件夹中)?
I need C or C++ code but I am stuck. I do not know how to solve this.
我需要 C 或 C++ 代码,但我被卡住了。我不知道如何解决这个问题。
采纳答案by Abhijeet Rastogi
From the inotify manpage:
从 inotify 联机帮助页:
IN_CREATE File/directory created in watched directory (*).
It can be done by catching this event.
可以通过捕获此事件来完成。
Again from the manpage:
再次从联机帮助页:
Limitations and caveats
Inotify monitoring of directories is not recursive: to monitor subdirectories under a directory, additional watches must be created. This can take a significant
amount time for large directory trees.
So, you will need to do the recursive part yourself. You can start by looking an example from here. You should also have a look at the project notify-tools
所以,你需要自己做递归部分。您可以从此处查看示例开始。您还应该查看项目通知工具
EXAMPLE as asked in comments: It monitors /tmp/inotify1
& /tmp/inotify2
for new files created & displays the events
如评论中要求的示例:它监视/tmp/inotify1
和/tmp/inotify2
创建新文件并显示事件
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/inotify.h>
#define EVENT_SIZE ( sizeof (struct inotify_event) )
#define BUF_LEN ( 1024 * ( EVENT_SIZE + 16 ) )
int main( int argc, char **argv )
{
int length, i = 0;
int fd;
int wd[2];
char buffer[BUF_LEN];
fd = inotify_init();
if ( fd < 0 ) {
perror( "inotify_init" );
}
wd[0] = inotify_add_watch( fd, "/tmp/inotify1", IN_CREATE);
wd[1] = inotify_add_watch (fd, "/tmp/inotify2", IN_CREATE);
while (1){
struct inotify_event *event;
length = read( fd, buffer, BUF_LEN );
if ( length < 0 ) {
perror( "read" );
}
event = ( struct inotify_event * ) &buffer[ i ];
if ( event->len ) {
if (event->wd == wd[0]) printf("%s\n", "In /tmp/inotify1: ");
else printf("%s\n", "In /tmp/inotify2: ");
if ( event->mask & IN_CREATE ) {
if ( event->mask & IN_ISDIR ) {
printf( "The directory %s was created.\n", event->name );
}
else {
printf( "The file %s was created.\n", event->name );
}
}
}
}
( void ) inotify_rm_watch( fd, wd[0] );
( void ) inotify_rm_watch( fd, wd[1]);
( void ) close( fd );
exit( 0 );
}
Test run:
测试运行:
shadyabhi@archlinux ~ $ ./a.out
In /tmp/inotify1:
The file abhijeet was created.
In /tmp/inotify2:
The file rastogi was created.
^C
shadyabhi@archlinux ~ $