文件管理 · 2022年7月25日

linux监控文件修改|linux如何查看谁修改文件

① java如何在linux下监听某个目录下是否有文件改变

JDK 7 的nio2 WatchService可以监听文件系统。

Oracle官方教程链接 http://docs.oracle.com/javase/tutorial/essential/io/notification.html

样例代码:

importstaticjava.nio.file.StandardWatchEventKinds.*;Pathpath=Paths.get("/home");WatchServicewatchService=FileSystems.getDefault().newWatchService();WatchKeywatchKey=path.register(watchService,ENTRY_CREATE,ENTRY_DELETE,ENTRY_MODIFY);/*privatebooleannotDone=true;while(notDone){try{WatchKeywatchKey=watchService.poll(60,TimeUnit.SECONDS);List<WatchEvent.Kind<?>>events=watchKey.pollEvents();for(WatchEventevent:events){//.registerPathwatchedPath=(Path)watchKey.watchable();//returnstheeventtype=event.kind();//returnsthecontextoftheeventPathtarget=(Path)event.context();}if(!watchKey.reset()){…handlesituationnolongervalid}}catch(InterruptedExceptione){Thread.currentThread().interrupt();}}*/

② linux如何查看最近修改的文件

Find应用实例查找最近30分钟修改的当前目录下的.php文件find . -name '*.php' -mmin -30查找最近24小时回修改答的当前目录下的.php文件find . -name '*.php' -mtime 0查找最近24小时修改的当前目录下的.php文件,并列出详细信息find . -name '*.inc' -mtime 0 -ls查找当前目录下,最近24-48小时修改过的常规文件。find . -type f -mtime 1查找当前目录下,最近1天前修改过的常规文件。find . -type f -mtime +1

③ linux如何查看谁修改文件

只能查看修改时间记录,不能查看是谁改的。如果不想别人改,可以给文件设置权限。1,打开终端,输入chmod fileName 744。2,使用其他用户登录尝试修改是否成功。

④ linux 查看文件修改时间

find/-mtime-7查到系统中七天内修过过的文件find/-mtime7查看过去的第七天修改过的文件,我测试时,好像都会多一天,就是第8天修改的要是写3的话,可能是过去第4天

⑤ 在linux系统下,编写一个shell脚本 实现对文件的监控

如果文件比较小,你可以备份原文件用diff来对比判断。因为不知道你具体是想通过什么专对比,不好属说。你可以通过tripwire这个软件来检测,具体安装方法自己网络,不细说了。如果自己写你必须要找到一些判断内容,比如时间,容量,如果文件包含时间戳你就可以判断时间戳来截取文件内容。自己写的话就需要具体情况具体分析。

⑥ linux怎么查看谁修改了文件

文件只有修改时间,没有修改用户的不过你如果是管理员可以查看各用户的bash操作记录

⑦ 跪求linux 下的一个shell脚本来监控指定的目录及其子目录中的任何文件变化然后报告到指定记录文件。

我可以给你提供个思路,可以实现你的需求,同时你自己写出来也专是一个不错的学习机属会,我写过一个“一点触发多点生效”的shell脚本,应用到inotify技术,你可以参考下语法应用。http://hi..com/cong_rong520/blog/item/563ce16e8f0bc8e442169482.html

⑧ 在linux下使用inotify监控,能不能够知道监控目录下子目录中是哪个文件被修改了。。。求方法。。。

这个我们期末考试考过。inotify只能监控单层目录变化,不能监控子目录中的变化情况。如果需要监控子目录,需要在调用inotify_add_watch(int fd, char *dir, int mask):int建立监控时,递归建立子目录的监控,伪代码如下void addwatch(int fd, char *dir, int mask){ wd = inotify_add_watch(fd, dir, mask); 向目录集合加入(wd, dir); for (dir下所有的子目录subdir) addwatch(fd, subdir, mask);}这样就可以得到一个目录集合,其中每一个wd对应一个子目录。当你调用read获取信息时,可以得到一个下面的结构体struct inotify_event{ int wd; /* Watch descriptor. */ uint32_t mask; /* Watch mask. */ uint32_t cookie; /* Cookie to synchronize two events. */ uint32_t len; /* Length (including NULs) of name. */ char name __flexarr; /* Name. */};其中,通过event->wd和刚才记录的目录集合可以知道变动的具体子目录。event->name为具体的文件名称。event->name是一个char name[0]形式的桩指针,具体的name占据的长度可以由event->len得出我的监控部分代码如下:enum {EVENT_SIZE = sizeof(struct inotify_event)};enum {BUF_SIZE = (EVENT_SIZE + 16) << 10};void watch_mon(int fd){ int i, length; void *buf; struct inotify_event *event; buf = malloc(BUF_SIZE); while ((length = read(fd, buf, BUF_SIZE)) >= 0) { i = 0; while (i < length) { event = buf + i; if (event->len) 具体处理函数(event); i += EVENT_SIZE + event->len; } } close(fd); exit(1);}在你的具体处理函数中,通过wd辨识子目录,通过name辨识文件 这是利用C++STLmap写的一个范例,可以监视当前目录下(含子目录)的变化,创建,删除过程(新建立的目录不能监视,只能通过监视到创建新目录的事件后重新初始化监视表)新版1.1.0,可以监视创建的子目录,方法是,当do_action探测到新目录创建的动作时,调用inotify_add_watch追加新的监视/* Copyright (C) 2010-2011 LIU An ([email protected]) This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a of the GNU General Public License along with this program. If not, see <http://www.gnu.org/licenses/>.*/#include <stdio.h>#include <stdlib.h>#include <string.h>#include <unistd.h>#include <sys/types.h>#include <sys/inotify.h>#include <errno.h>#include <dirent.h>#include <map>#include <string>using namespace std;void addwatch(int, char*, int);static int filter_action(uint32_t mask);int watch_init(int mask, char *root);void addwatch(int fd, char *dir, int mask);static void do_action(int fd, struct inotify_event *event);void watch_mon(int fd);static void send_mess(char *name, char *act, int ewd);void append_dir(int fd, struct inotify_event *event, int mask);map<int, string> dirset;enum{MASK = IN_MODIFY | IN_CREATE | IN_DELETE};int main(int argc, char **argv){ int fd; if (argc != 2) { fprintf(stderr, "Usage: %s dir\n", argv[0]); exit(1); } fd = watch_init(MASK, argv[1]); watch_mon(fd); return 0;}int watch_init(int mask, char *root){ int i, fd; if ((fd = inotify_init()) < 0) perror("inotify_init"); addwatch(fd, root, mask); return fd;}void addwatch(int fd, char *dir, int mask){ int wd; char subdir[512]; DIR *odir; struct dirent *dent; if ((odir = opendir(dir)) == NULL) { perror("fail to open root dir"); exit(1); } wd = inotify_add_watch(fd, dir, mask); dirset.insert(make_pair(wd, string(dir))); errno = 0; while ((dent = readdir(odir)) != NULL) { if (strcmp(dent->d_name, ".") == 0 || strcmp(dent->d_name, "..") == 0) continue; if (dent->d_type == DT_DIR) { sprintf(subdir, "%s/%s", dir, dent->d_name); addwatch(fd, subdir, mask); } } if (errno != 0) { perror("fail to read dir"); exit(1); } closedir (odir);}enum {EVENT_SIZE = sizeof(struct inotify_event)};enum {BUF_SIZE = (EVENT_SIZE + 16) << 10};void watch_mon(int fd){ int i, length; void *buf; struct inotify_event *event; buf = malloc(BUF_SIZE); while ((length = read(fd, buf, BUF_SIZE)) >= 0) { i = 0; while (i < length) { event = (struct inotify_event*)(buf + i); if (event->len) do_action(fd, event); i += EVENT_SIZE + event->len; } } close(fd); exit(1);}static char action[][10] ={ "modified", "accessed", "created", "removed"};enum{NEWDIR = IN_CREATE | IN_ISDIR};static void do_action(int fd, struct inotify_event *event){ int ia, i; if ((ia = filter_action(event->mask)) < 0) return; if ((event->mask & NEWDIR) == NEWDIR) append_dir(fd, event, MASK); send_mess(event->name, action[ia], event->wd);}void append_dir(int fd, struct inotify_event *event, int mask){ char ndir[512]; int wd; sprintf(ndir, "%s/%s", dirset.find(event->wd)->second.c_str(), event->name); wd = inotify_add_watch(fd, ndir, mask); dirset.insert(make_pair(wd, string(ndir)));}static int filter_action(uint32_t mask){ if (mask & IN_MODIFY) return 0; if (mask & IN_ACCESS) return 1; if (mask & IN_CREATE) return 2; if (mask & IN_DELETE) return 3; return -1;}static void send_mess(char *name, char *act, int ewd){ char format[] = "%s was %s.\n"; char file[512]; sprintf(file, "%s/%s", dirset.find(ewd)->second.c_str(), name); printf(format, file, act);}参考资料是我们作业的提交,没有考虑递归创建子目录监控的问题。

⑨ Linux gcc或者g++编程 下如何监视文件夹中文件的改变

从编程实现上来说,linux下做这个事情,要比windows上做简单,windows上有相关API提供接口,比如VC下的ReadDirectoryChanges函数,但其实它的功能并不是很完善,我之前曾经做过一个windows下类似的工具,如果要做到很完善的功能,要涉及到windows底层驱动。而linux下做这个事情就简单多了,从内核2.6.13开始,inotify编程接口就已经被加入到系统内核中,用来监控文件系统事件,它能监控到的事件包含文件/文件夹的新建,删除,修改,属性被修改,打开等等,功能比windows API提供的要丰富得多。在linux下输入man inotify即可查看该接口的编程手册,要实现对某个文件夹的监控,只需要使用该接口提供的inotify_init, inotify_add_watch, inotify_rm_watch三个函数即可。具体可以网络inotify,很容易就能找到相关示例代码。

附Linux下inotify编程接口部分截图:

点击图片可以看大图。