日期:2014-05-16  浏览次数:20650 次

linux 系统下查看文件操作的详细信息

之前有位大牛问我一个问题,在Linux系统下想查看一个目录下的所有文件中在 昨天11:00 到今天 10:00 被修改过的。

把这些文件给找出来,应该怎样去操作

我以为ll可以

原来我错了,SO 被BS

今天不忙,就着公司的Linux机器,把处理这个问题的脚本整理一下:

第一步,编写shell脚本:

[root@kaifa02 cxr]# more wctxt.sh 
#!/bin/bash
Directory_Path=/home/cxr
cd $Directory_Path
Files=$(ls *.txt)
echo "-------------the begin ----------"+$Files
for file in $Files 
do echo $(stat $file) >>stat.log
done 
cd -
echo "------------the end ----------------"

?

这样就把文件的详细信息放到了 stat.log 文件中,信息如下所示:

[root@kaifa02 cxr]# more stat.log 
File: `a.txt' Size: 2717 Blocks: 8 IO Block: 4096 regular file Device: 803h/2051d Inode: 7242028 Links: 1 Access: (0644/-rw-r--r--) 
Uid: ( 0/ root) Gid: ( 0/ root) Access: 2010-08-12 16:13:59.000000000 +0800 Modify: 2009-06-05 10:03:51.000000000 +0800 Change: 2010
-08-12 16:51:58.000000000 +0800
File: `cntlog0508.txt' Size: 0 Blocks: 0 IO Block: 4096 regular empty file Device: 803h/2051d Inode: 7242026 Links: 1 Access: (0644/
-rw-r--r--) Uid: ( 0/ root) Gid: ( 0/ root) Access: 2010-08-12 16:13:59.000000000 +0800 Modify: 2009-05-11 15:25:26.000000000 +0800 
Change: 2010-08-12 16:51:58.000000000 +0800
File: `log.txt' Size: 38 Blocks: 8 IO Block: 4096 regular file Device: 803h/2051d Inode: 7242029 Links: 1 Access: (0644/-rw-r--r--) 
Uid: ( 0/ root) Gid: ( 0/ root) Access: 2010-09-17 14:50:46.000000000 +0800 Modify: 2010-09-17 14:50:46.000000000 +0800 Change: 2010
-09-17 14:50:46.000000000 +0800
File: `tt.txt' Size: 225 Blocks: 8 IO Block: 4096 regular file Device: 803h/2051d Inode: 7242027 Links: 1 Access: (0644/-rw-r--r--) 
Uid: ( 0/ root) Gid: ( 0/ root) Access: 2010-08-12 16:13:59.000000000 +0800 Modify: 2008-09-04 11:23:41.000000000 +0800 Change: 2010
-08-12 16:51:58.000000000 +0800
File: `t.txt' Size: 247 Blocks: 8 IO Block: 4096 regular file Device: 803h/2051d Inode: 7242022 Links: 1 Access: (0644/-rw-r--r--) U
id: ( 0/ root) Gid: ( 0/ root) Access: 2010-08-12 16:13:59.000000000 +0800 Modify: 2008-12-10 16:15:07.000000000 +0800 Change: 2010-
08-12 16:51:52.000000000 +0800
[root@kaifa02 cxr]# 

?

上面的信息是有一定格式的,再想得到文件名称和具体修改时间就很简单了

第二步:也可以将这个步骤写到shell脚本中,那样的出来的数据可以直接操作了?

[root@kaifa02 cxr]# cat stat.log  |awk -F ': '  '{print $2,$13;}' |awk -F ' ' '{print $1,$3,$4}'
`a.txt' 2009-06-05 10:03:51.000000000
`cntlog0508.txt' 2009-05-11 15:25:26.000000000
`log.txt' 2010-09-17 14:50:46.000000000
`tt.txt' 2008-09-04 11:23:41.000000000
`t.txt' 2008-12-10 16:15:07.000000000

?第三步,有了这些数据,进行时间的比对就好弄一些了。

?可以定时的使用java程序去跑上面第二步出来的数据,然后把数据记录到数据库里面或特定的文件目录里面。

?不知道Linux 下有没有可以对字符串的时间进行大小比较的命令,如果有的话,直接使用命令操作,那一个脚本就可以把这个需求搞定了--- 待研究。