-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdiffdir
executable file
·111 lines (90 loc) · 1.88 KB
/
diffdir
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
#!/bin/bash
#
# diffdir
#
cmdline=diffdir
usagestr=$(
cat <<EOF
diffdir [options] d1 d2
d1 - first directory
d2 - second directory
Compares the text files in two directories and prints the names of
the files that differ to stdout.
Options
-------
-n depth of search, where "-n 1" is topmost directory.
space between switch and number is mandatory.
Default depth is recursion through all subdirectories.
-h this help text
\0
EOF
)
usage() {
echo -e "$usagestr"
exit 0
}
# rm_topdir - removes the top directory name, leaving only the sub
# directories
# $1 - pathname
# $2 - return pathname with top directory stripped off
#
rm_topdir() {
local path="$1"
local len;
[[ "${path:0:1}" == "/" ]] && path="${path:1}"
[[ "${path:0:2}" == "./" ]] && path=${path:2}
path="$(echo "$path" | cut -d'/' -f2-)"
len=${#path}
((len--))
[[ -d "$path" ]] && [[ "${path:$len:1}" != "/" ]] && path="$path""/"
eval $2=$path
}
# is_diff() - Compares two files. If they are different, prints the full
# path names of the files side-by-side to stdout.
#
# $1 - first file
# $2 - second file
#
is_diff() {
local f1=$1
local f2=$2
diff $f1 $f2 1> /dev/null
[[ $? != 0 ]] && echo -e ""$f1"\t"$f2""
}
[ $# -gt 0 ] || usage
declare dir1
declare dir2
declare outfile
declare maxdepth
declare maxdepthstr=""
while getopts "n:h" OPTION; do
case "$OPTION" in
n ) maxdepth=$OPTARG
maxdepthstr="-maxdepth $maxdepth"
shift
shift
;;
h ) usage
;;
* ) echo "unrecognized option"
echo "$usage"
exit 1
;;
esac
done
dir1="$1"
dir2="$2"
export -f rm_topdir
export -f is_diff
export dir1
export dir2
find $dir1 $maxdepthstr -type f \
-exec grep -Iq . {} \; \
-exec sh -c \
' \
declare fspec; \
rm_topdir $1 fspec; \
ls $dir2/$fspec > /dev/null 2>&1; \
[[ $? == 0 ]] && is_diff $dir1/$fspec $dir2/$fspec; \
' \
sh {} \;