应用背景
在进行项目开发时,需要对某一个目录下的多个文件进行修改,如果我们想知道相比上一个版本,哪些文件发生了变动,该如何做呢?
解决方案
可以使用perl脚本来实现这个功能:
1. 在修改工程文件前,先备份为prj_bak
2. 在prj目录下修改工程文件
3. 运行脚本 ./get_changed_file.pl prj/ prj_bak/
脚本/get_changed_file.pl内容如下:
1 #!/usr/bin/perl -w
2 use File::Basename;
3
4 my $dir1=$ARGV[0];
5 my $dir2=$ARGV[1];
6 my $blank='';
7
8 system("find ./$dir1 -type f | sort -n | xargs openssl md5 > a.txt");
9 system("find ./$dir2 -type f | sort -n | xargs openssl md5 > b.txt");
10
11 open my $FH_DIR1,'<',"a.txt" or die;
12 open my $FH_DIR2,'<',"b.txt" or die;
13 open my $FH_OUT,'>',"output.txt" or die;
14 while(<$FH_DIR1>)
15 {
16 chomp;
17 my $dir1_filename_with_path = $blank;
18 my $dir1_filename_no_path = $blank;
19 my $dir1_hash = $blank;
20 my $dir2_filename_with_path = $blank;
21 my $dir2_filename_no_path = $blank;
22 my $dir2_hash = $blank;
23 if(/(\(.*)\)=\s(\w+)/)
{
25 $dir1_filename_with_path = $1;
26 $dir1_filename_no_path = basename($1);
27 #print "dir1_filename: ",$dir1_filename_with_path,"\n";
28 $dir1_hash = $2;
29 #print $dir1_hash,"\n";
30 }
31 my $dir2_line = <$FH_DIR2>;
32 chomp $dir2_line;
33 if($dir2_line =~ /\((.*)\)=\s(\w+)/)
34 {
35 $dir2_filename_with_path = $1;
36 $dir2_filename_no_path = basename($1);
37 #print "dir2_filename: ",$dir2_filename_with_path,"\n";
38 $dir2_hash = $2;
39 #print $dir2_hash,"\n";
40 }
41 if($dir1_filename_no_path eq $dir2_filename_no_path)
42 {
43 #print "filename match, continue...\n" ;
44 if($dir1_hash eq $dir2_hash)
45 {
46 print "$dir1_filename_with_path compare to $dir2_filename_with_path: NO CHANGE,continue...\n";
47 }
48 else
49 {
50 print($FH_OUT "$dir1_filename_no_path\n");
51 }
52 }
53 else
54 {
55 print "Filename Mismatch, exit...\n" ;
56 exit 7;
57 }
58 }
59 close($FH_DIR1);
60 close($FH_DIR2);
61 close($FH_OUT);
62 system("rm -rf a.txt");
63 system("rm -rf b.txt");