-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcheck_same_dir.pl
More file actions
executable file
·95 lines (77 loc) · 2.08 KB
/
check_same_dir.pl
File metadata and controls
executable file
·95 lines (77 loc) · 2.08 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
#!/usr/bin/perl -w
use 5.18.0;
$| = 1;
unless ( $#ARGV == 1 ) {
say "$0: Needs exactly 2 directories as parameters.";
exit 1;
}
$ARGV[0] =~ s/\/$// while ( $ARGV[0] =~ /\/$/);
$ARGV[1] =~ s/\/$// while ( $ARGV[1] =~ /\/$/);
my %differents_files;
my $cpt = 0;
my $nb_check = 0;
print "Differences found : $cpt";
same_files(@ARGV);
same_files($ARGV[1], $ARGV[0]);
sub same_files {
my ($dir1, $dir2) = @_;
$nb_check++;
foreach my $file1 (glob("$dir1/*")) {
my ($end) = $file1 =~ /$dir1(.*)/;
my $file2 = "$dir2$end";
if (-f $file1) {
if (! -f $file2) {
print "\rDifferences found : " . ++$cpt;
$differents_files{$file1} = 2;
} elsif (! same_content($file1, $file2) ) {
print "\rDifferences found : " . ++$cpt;
$differents_files{$file1} = 3 unless exists $differents_files{$file2};
}
}
elsif (-d $file1) {
if (-d $file2) {
same_files($file1, $file2);
} else {
print "\rDifferences found : " . ++$cpt;
$differents_files{$file1} = 1;
}
}
}
}
sub same_content {
my ($file1, $file2) = @_;
open my $fp1, '<', $file1 or die $!;
open my $fp2, '<', $file2 or die $!;
foreach my $line1 (<$fp1>) {
my $line2 = <$fp2>;
unless ($line1 eq $line2) {
close $fp1;
close $fp2;
return;
}
}
close $fp1;
close $fp2;
return 1
}
print "\r" . " " x 40 . "\r";
my ($dirs, $files, $contents) = ("", "", "");
while (my ($file, $value) = (each %differents_files)) {
$dirs .= "\t$file\n" if $value == 1;
$files .= "\t$file\n" if $value == 2;
$contents .= "\t$file\n" if $value == 3;
}
if ($dirs) {
say "Missing directories:";
print $dirs;
}
if ($files) {
say "Missing files:";
print $files;
}
if ($contents) {
say "Differents contents:";
print $contents;
}
say "\n$nb_check files or directories checked.";
say "$cpt differences found.\n";