免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
12下一页
最近访问板块 发新帖
查看: 3513 | 回复: 13
打印 上一主题 下一主题

[日期时间] 檔案時間差異比較 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2013-04-15 16:13 |只看该作者 |正序浏览
有一個有關檔案時間比較的問題想請教一下,如何比對下列檔案時間,每小時比較一次
若時間有變化即列出檔案。


-rwxr-xr-x   1 hslusrp  sys      274327 Dec 27 14:36 cpsmplab
-rwxr-xr-x   1 hslusrp  sys      668375 Jan 10 13:18 cpsoccal
-rwxr-xr-x   1 hslusrp  sys      251373 Dec 20 2007  cpspolct
-rwxr-xr-x   1 hslusrp  sys      249627 Dec 20 2007  cpspreqg
-rwxr-xr-x   1 hslusrp  sys      387538 Oct 27 2011  cpstbmql
-rwxr-xr-x   1 hslusrp  sys      266816 Oct 08 2012  cpstbprd
-rwxr-xr-x   1 hslusrp  sys      285574 Feb 22 15:05 cpveqpln
-rwxr-xr-x   1 hslusrp  3sys      369558 Mar 08 2011  cpvryoop


請問一下個位高手有辦法可以每小時比較一次檔案時間是否有被修改過,
若有修改即列出修改檔案。

论坛徽章:
0
14 [报告]
发表于 2013-04-16 17:52 |只看该作者
你的系统是不是不支持stat啊?你可以用C或者其他语言自己实现一下,不是很复杂的功能的,,,

论坛徽章:
0
13 [报告]
发表于 2013-04-16 17:31 |只看该作者
本帖最后由 68930089 于 2013-04-16 17:37 编辑

$VAR1 = [
          'ain',
          'chabeprc',
          'chaltprc',
          'chbcdrpt',
          'chbdlsht',
          'chcaryox',
          'chccrrhs',
          'chceqmhs',
          'chceqpnt',
          'chcrinsp',
          'chdefprc',
          'chdeldel',
          'chdeldel.sh',
          'chdeleqp',
          'chdelope',
          'cheaactc',
          'chedcprc',
          'cheenprc',
          'chemdprc',
          'cheqlmhs',
          'cheqputl',
          'chestprc',
          'chis-del-del.sh',
          'chis-del-edc.sh',
          'chis-del-eqp.sh',
          'chis-del-lot.sh',
          'chis-del-ope.sh',
          'chis-del-sht.sh',
          'chloiprc',
          'chmtlprc',
          'chmtrlmt',
          'chmtrlst',
          'choplcat.c',
          'chopmain.c',
          'chopprc.bak',
          'chopproc',
          'chopproc.bak',
          'chopproc.tar',
          'chqcdeft',
          'chshiphs',
          'chshiphs.bak',
          'chshtcon',
          'chshtprc',
          'chsmnthis',
          'chsrtcfg',
          'chvideft',
          'chvpklhs',
          'chwokrem',
          'cpcmsrdb',
          'del_hcwip_count.log',
          'dhshtprc',
          'dist',
          'lcdhisexe.tar',
          'nohup.out',
          'qdatakill',
          'snap.ksh',
          'test2',
          'zzzz.tmp'
        ];
$VAR1 = {
          'dhshtprc' => 'dist',
          'chdefprc' => 'chdeldel',
          'chdelope' => 'cheaactc',
          'lcdhisexe.tar' => 'nohup.out',
          'test2' => 'zzzz.tmp',
          'chvpklhs' => 'chwokrem',
          'chemdprc' => 'cheqlmhs',
          'chshiphs.bak' => 'chshtcon',
          'chis-del-del.sh' => 'chis-del-edc.sh',
          'choplcat.c' => 'chopmain.c',
          'chaltprc' => 'chbcdrpt',
          'chdeldel.sh' => 'chdeleqp',
          'chopprc.bak' => 'chopproc',
          'chmtrlmt' => 'chmtrlst',
          'chis-del-eqp.sh' => 'chis-del-lot.sh',
          'chbdlsht' => 'chcaryox',
          'chccrrhs' => 'chceqmhs',
          'cpcmsrdb' => 'del_hcwip_count.log',
          'chopproc.bak' => 'chopproc.tar',
          'cheqputl' => 'chestprc',
          'ain' => 'chabeprc',
          'chedcprc' => 'cheenprc',
          'chloiprc' => 'chmtlprc',
          'chceqpnt' => 'chcrinsp',
          'qdatakill' => 'snap.ksh',
          'chshtprc' => 'chsmnthis',
          'chsrtcfg' => 'chvideft',
          'chqcdeft' => 'chshiphs',
          'chis-del-ope.sh' => 'chis-del-sht.sh'
        };


執行之後出現上面資料,求救啊老闆要求要監控檔案系統變化。
本是對這個不熟所以才來這裡求助的。

论坛徽章:
0
12 [报告]
发表于 2013-04-16 17:10 |只看该作者
这个只是一个示例,代码里面么有考虑删除等情况,一旦删除后文件stat获取的时间值不可知。。。此时hash比较就会有问题,你可以用你熟悉的语言写一个类似版本,这个只是一个例子。。。

论坛徽章:
0
11 [报告]
发表于 2013-04-16 17:02 |只看该作者
加个dumper试试,是不是你的系统里面(stat($file))[9]不是最后修改的时间。。。
perldoc -f stat看看相关东西。。。
  1. #! /usr/bin/perl

  2. use strict;
  3. use warnings;
  4. use Data::Dumper;

  5. use constant SLEEP_TIME => 10;

  6. my $dir = ".";
  7. my @files = map { (my $x = $_) =~ s/\n//; $x } `ls $dir`;
  8. my %file_access = map { $_, (stat($_))[9] } @files;

  9. print Dumper(\@files);
  10. print Dumper(\%file_access);
  11. exit;
  12. while (1) {
  13.   sleep SLEEP_TIME;

  14.   foreach (@files) {
  15.     my $file = $_;
  16.     my $last_modify = (stat($file))[9];

  17.     if ($file_access{$file} != $last_modify) {
  18.       print "\t", $file, "\n";
  19.       $file_access{$file} = $last_modify;
  20.     }
  21.   }
  22. }

复制代码

论坛徽章:
0
10 [报告]
发表于 2013-04-16 16:09 |只看该作者
本帖最后由 68930089 于 2013-04-16 16:13 编辑

+1  #! /usr/bin/perl
    +2
    +3
    +4
    +5  #use strict;
    +6
    +7  use warnings;
    +8  use Net::SMTP;
    +9
   +10
   +11  use constant SLEEP_TIME => 6;
   +12
   +13
   +14
   +15  my $dir = "/prod/his/exe";
   +16
   +17  my @files = map { (my $x = $_) =~ s/\n//; $x } `ls $dir`;
   +18
   +19  my %file_access = map { $_, (stat($_))[9] } @files;
   +20
   +21
   +22  while (1) {
   +23
   +24    sleep SLEEP_TIME;
   +25
   +26
   +27
   +28    foreach (@files) {
   +29
   +30      my $file = $_;
   +31
   +32      my $last_modify = (stat($file))[9];
   +33
   +34      if ($file_access{$file} != $last_modify) {
   +35
   +36        print "\t", $file, "\n";
   +37
   +38
   +39        $file_access{$file} = $last_modify;
   +40
   +41     }
   +42
   +43    }
   +44
   +45  }


Use of uninitialized value in numeric ne (!=) at test.pl line 34.
Argument "bkup" isn't numeric in numeric ne (!=) at test.pl line 34.
Use of uninitialized value in numeric ne (!=) at test.pl line 34.
Use of uninitialized value in numeric ne (!=) at test.pl line 34.
Use of uninitialized value in numeric ne (!=) at test.pl line 34.
Argument "chaltprc" isn't numeric in numeric ne (!=) at test.pl line 34.
Use of uninitialized value in numeric ne (!=) at test.pl line 34.
Use of uninitialized value in numeric ne (!=) at test.pl line 34.
Use of uninitialized value in numeric ne (!=) at test.pl line 34.

论坛徽章:
0
9 [报告]
发表于 2013-04-16 16:02 |只看该作者
Use of uninitialized value in numeric ne (!=) at test.pl line 38.
Argument "chopproc.20080814" isn't numeric in numeric ne (!=) at test.pl line 38.
Use of uninitialized value in numeric ne (!=) at test.pl line 38.
Use of uninitialized value in numeric ne (!=) at test.pl line 38.
Use of uninitialized value in numeric ne (!=) at test.pl line 38.
Argument "chopproc.bak" isn't numeric in numeric ne (!=) at test.pl line 38.
Use of uninitialized value in numeric ne (!=) at test.pl line 38.
Use of uninitialized value in numeric ne (!=) at test.pl line 38.
Use of uninitialized value in numeric ne (!=) at test.pl line 38.
Argument "chqcdeft" isn't numeric in numeric ne (!=) at test.pl line 38.
Use of uninitialized value in numeric ne (!=) at test.pl line 38.
Use of uninitialized value in numeric ne (!=) at test.pl line 38.
Use of uninitialized value in numeric ne (!=) at test.pl line 38.
Argument "chshiphs.bak" isn't numeric in numeric ne (!=) at test.pl line 38.
Use of uninitialized value in numeric ne (!=) at test.pl line 38.
Use of uninitialized value in numeric ne (!=) at test.pl line 38.
Use of uninitialized value in numeric ne (!=) at test.pl line 38.
Argument "chshtprc" isn't numeric in numeric ne (!=) at test.pl line 38.
Use of uninitialized value in numeric ne (!=) at test.pl line 38.
Use of uninitialized value in numeric ne (!=) at test.pl line 38.
Use of uninitialized value in numeric ne (!=) at test.pl line 38.
Argument "chsrtcfg" isn't numeric in numeric ne (!=) at test.pl line 38.
Use of uninitialized value in numeric ne (!=) at test.pl line 38.
Use of uninitialized value in numeric ne (!=) at test.pl line 38.
Use of uninitialized value in numeric ne (!=) at test.pl line 38.
Argument "chvpklhs" isn't numeric in numeric ne (!=) at test.pl line 38.

出現上面訊息請問一下要怎麼解啊

论坛徽章:
0
8 [报告]
发表于 2013-04-16 10:15 |只看该作者
#!/bin/bash
stat -c "%Y:%n" /you/dir/* > stat.old
while :
do
   stat -c "%Y:%n" /you/dir/* > stat.new
   for file in $(<stat.new)
     do
        grep -q $file stat.old || echo ${file#*:} >> result
     done
   cat stat.new > stat.old && rm -fr stat.new
   sleep 3600
done

论坛徽章:
0
7 [报告]
发表于 2013-04-16 08:58 |只看该作者
嗯…測試過了,真的可行。
my $dir = "."; <-- 這裡是指目錄的位置,那如果要
比較多個資料匣的話可行嗎?

论坛徽章:
0
6 [报告]
发表于 2013-04-15 22:37 |只看该作者
一个简单的模拟的perl例子。。。
  1. #! /usr/bin/perl

  2. use strict;
  3. use warnings;

  4. use constant SLEEP_TIME => 10;

  5. my $dir = ".";
  6. my @files = map { (my $x = $_) =~ s/\n//; $x } `ls $dir`;
  7. my %file_access = map { $_, (stat($_))[9] } @files;

  8. while (1) {
  9.   sleep SLEEP_TIME;

  10.   foreach (@files) {
  11.     my $file = $_;
  12.     my $last_modify = (stat($file))[9];
  13.     if ($file_access{$file} != $last_modify) {
  14.       print "\t", $file, "\n";
  15.       $file_access{$file} = $last_modify;
  16.     }
  17.   }
  18. }

复制代码
  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP