This repository has been archived by the owner on Jun 28, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathcmptree
executable file
·131 lines (114 loc) · 2.51 KB
/
cmptree
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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
#!/bin/bash
#
# cmptree: compare directory trees recursively and report the differences.
# Author: Ives Aerts
function gettype () {
if [ -L "$1" ]; then
echo "softlink"
elif [ -f "$1" ]; then
echo "file"
elif [ -d "$1" ]; then
echo "directory"
elif [ -c "$1" -o -b "$1" ]; then
echo "special"
elif [ -p "$1" ]; then
echo "pipe"
else
echo "unknown"
fi
}
function exists () {
if [ -e "$1" -o -L "$1" ]; then
return 0;
else
# echo "$1 does not exist."
return 1;
fi
}
function comparefile () {
[ "$1" = "/var/swap" ] && return
cmp -s "$1" "$2"
if [ $? -gt 0 ]; then
# if [ -z "$(file "$1" | grep text)" ]; then
echo "changed: $1 different from $2"
# else
# echo
# diff -u "$1" "$2"
# echo
# fi
# else
# echo "$1 same as $2"
fi
return
}
function comparedirectory () {
local result=0
#find "$1" -maxdepth 1 -print
#echo
# (find "$1" -maxdepth 1 -printf "%f\n" | sed 1d && find "$2" -maxdepth 1 -printf "%f\n" | sed 1d) | sort | uniq | while read i ; do
(find "$1" -maxdepth 1 ! -path . -printf "%f\n" && find "$2" -maxdepth 1 ! -path . -printf "%f\n") | sort | uniq | while read i ; do
compare "$1/$i" "$2/$i" || result=1
done
return $result
}
function comparesoftlink () {
local dest1=`ls -l "$1" | awk '{ print $11 }'`
local dest2=`ls -l "$2" | awk '{ print $11 }'`
if [ "$dest1" = "$dest2" ]; then
return 0
else
echo "different link targets $1 -> $dest1, $2 -> $dest2"
return 1
fi
}
# compare a file, directory, or softlink
function compare () {
#echo ----"$1"
# (exists "$1" && exists "$2") || return 1;
if ! ( exists "$1" && exists "$2" ); then
exists "$1" && echo "deleted: $1"
exists "$2" && echo "added: $2"
return 1;
fi
local type1=$(gettype "$1")
local type2=$(gettype "$2")
if [ $type1 = $type2 ]; then
case $type1 in
file)
comparefile "$1" "$2"
;;
directory)
comparedirectory "$1" "$2"
;;
softlink)
comparesoftlink "$1" "$2"
;;
special)
true
;;
pipe)
true
;;
*)
echo "$1 of unknown type"
false
;;
esac
else
echo "type mismatch: $type1 ($1) and $type2 ($2)."
false
fi
return
}
if [ 2 -ne $# ]; then
cat << EOU
Usage: $0 dir1 dir2
Compare directory trees:
files are binary compared (cmp)
directories are checked for identical content
soft links are checked for identical targets
EOU
exit 10
fi
compare "$1" "$2"
exit $?