Linux系统是一个广泛使用的开放源代码操作系统,因其强大的功能和灵活性,深受许多开发人员和技术人员的喜爱。在Linux系统中,文件的修改记录是一个非常重要的功能,它可以帮助用户追踪文件的修改历史,以便更好地管理和维护文件。本篇文章将介绍如何在Linux系统中查找并记录文件的修改记录。
一、查找文件的修改记录
1. 使用命令行
在Linux系统中,可以使用命令行来查找文件的修改记录。以下是几个常用的命令行:
(1)stat命令:可以查看文件的修改时间、权限等信息。
语法:stat 文件名
例子:stat test.txt
(2)ls命令:可以查看文件的创建时间、修改时间等信息。
语法:ls -l 文件名
例子:ls -l test.txt
(3)checkinstall命令:可以查看安装过程中的更新日志。
语法:checkinstall –list
例子:checkinstall –list
2. 使用图形界面
在Linux系统中,也可以使用图形界面来查找文件的修改记录。以下是几个常用的图形界面:
(1)Nautilus文件管理器:可以通过右键单击文件,选择“属性”,然后点击“最后修改”选项卡来查看文件的修改时间和日期。
(2)Midnight Commander:可以通过打开文件,然后按下“F9”键,选择“属性”选项来查看文件的修改时间和日期。
二、记录文件的修改记录
1. 使用syslogd
在Linux系统中,syslogd是一个用于记录系统日志的守护进程。它可以记录许多系统事件,并可以将这些事件写入指定文件中。以下是记录文件的修改记录的步骤:
(1)编辑syslogd配置文件。
打开/etc/syslog.conf文件,并在其中添加以下行:
#记录文件的修改记录
kern.info /var/log/file.log
其中,“kern.info”表示记录的消息级别,“/var/log/file.log”表示记录的文件名称。
(2)重新启动syslogd守护进程。
输入以下命令重启syslogd守护进程:
/etc/init.d/syslogd restart
(3)测试修改文件。
修改文件,并使用以下命令查看文件的修改记录:
tl /var/log/file.log
2. 使用auditd
在Linux系统中,auditd是一个用于记录系统事件的守护进程。它可以记录用户、进程和文件的操作,以及系统资源的使用情况。以下是记录文件的修改记录的步骤:
(1)安装auditd工具。
输入以下命令安装auditd工具:
yum install audit
(2)编辑auditd配置文件。
打开/etc/audit/auditd.conf文件,并将以下行添加到文件末尾:
#记录文件的修改记录
-w /path/to/file -p war
其中,“/path/to/file”表示需要监控的文件路径,“-p war”表示监测文件的操作类型(写、追加和读)。
(3)重新启动auditd守护进程。
输入以下命令重启auditd守护进程:
systemctl restart auditd
(4)测试修改文件。
修改文件,并使用以下命令查看文件的修改记录:
auditctl -l | grep /path/to/file;ausearch -f /path/to/file
三、
在Linux系统中,记录文件的修改记录是非常重要的,可以帮助用户更好地管理和维护文件。本篇文章介绍了如何在Linux系统中查找和记录文件的修改记录,希望对各位用户有所帮助。
相关问题拓展阅读:
- 在linux下使用inotify监控,能不能够知道监控目录下子目录中是哪个文件被修改了。。。求方法。。。
- Linux 下怎么看到删除记录
在linux下使用inotify监控,能不能够知道监控目录下子目录中是哪个文件被修改了。。。求方法。。。
你可以试试自己用 mingw 编译一个 DOS 下面可用的 tail 啊~
如果你用 windows 的 cmd 。可以看看 powershell 有没有这个命令。
或者尝试安装一个 cygwin ,里面我记得有这个命令。之后用 cywin 的 bash 命令行当命令行用。
你要是用纯 DOS ,貌似你可以放弃计算机了……
这个我们期末考试考过。
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形式的桩指针,具体的name占据的长度可以由event->len得出
我的监控部分代码如下:
enum {EVENT_SIZE = sizeof(struct inotify_event)};
enum {BUF_SIZE = (EVENT_SIZE + 16) = 0)
{
i = 0;
while (i 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)LIU An ()
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 copy of the GNU General Public License
along with this program. If not, see .
*/
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
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 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);
exit(1);
}
fd = watch_init(MASK, argv);
watch_mon(fd);
return 0;
}
int watch_init(int mask, char *root)
{
int i, fd;
if ((fd = inotify_init()) 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) = 0)
{
i = 0;
while (i len)
do_action(fd, event);
i += EVENT_SIZE + event->len;
}
}
close(fd);
exit(1);
}
static char action =
{
“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)) mask & NEWDIR) == NEWDIR)
append_dir(fd, event, MASK);
send_mess(event->name, action, event->wd);
}
void append_dir(int fd, struct inotify_event *event, int mask)
{
char ndir;
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;
sprintf(file, “%s/%s”, dirset.find(ewd)->second.c_str(), name);
printf(format, file, act);
}
Linux 下怎么看到删除记录
如果只是想要查看最近用户使用删除命令删除的文件,其实可以使用history命令,该命令可以显示最近一段时间内执行过的操作命令,然后利用grep筛选出来:
history|grep rm
如果是程序或者进程后台进行删除的文件,或者系统内部删除的文件,也就无法通过上面的方法查找到最近删除的文件了,
但是如果删除的文件是在
linux系统
的ext2
文件系统
下的话,也可以使用debugfs命令来查看删除的文件:
1,首先查看需要恢复的文件所在的文件系统
命令行
模式下输入指令mount
# mount
/dev/xvda1 on / type ext2 (rw,errors=remount-ro)
proc on /proc type proc (rw,noexec,nosuid,nodev)
sysfs on /sys type sysfs (rw,noexec,nosuid,nodev)
由上知,/dev/xvda1挂载在/下,即
根目录
,且文件系统是ext2
2,将被删除的文件所在的分区重新挂载成只读
# mount -n -o remount,ro /dev/xvda1
3,使用debugfs工具查找删除的文件和恢复文件
# debugfs /dev/xvda1
debugfs 1.42 (29-Nov-2023)
debugfs: lsdel
进入debugfs模式后输入lsdel后可以看到被删除的文件信息
stat显示某个节点所对应的文件信息,
linux 文件被修改记录的介绍就聊到这里吧,感谢你花时间阅读本站内容,更多关于linux 文件被修改记录,如何查找并记录Linux文件的修改记录,在linux下使用inotify监控,能不能够知道监控目录下子目录中是哪个文件被修改了。。。求方法。。。,Linux 下怎么看到删除记录的信息别忘了在本站进行查找喔。
香港服务器首选树叶云,2H2G首月10元开通。
树叶云(www.IDC.Net)提供简单好用,价格厚道的香港/美国云服务器和独立服务器。IDC+ISP+ICP资质。ARIN和APNIC会员。成熟技术团队15年行业经验。
文章来源网络,作者:运维,如若转载,请注明出处:https://shuyeidc.com/wp/162446.html<