6c700d8f20a31e508b383295a416eb0d1ef812be
[feisty_meow.git] / scripts / files / compare_dirs.sh
1 #!/bin/bash
2
3 # compares the files and directory names in two different top-level directories
4 # and prints a report of the differences.
5
6 dir1="$1"; shift
7 dir2="$1"; shift
8
9 if [ -z "$dir1" -o -z "$dir2" ]; then
10   echo This script needs two directory names for which it will create a
11   echo list of differences in the two directory hierarchies.
12   exit 1
13 fi
14 #if [ ! -d "$dir1/" -o ! -d "$dir2/" ]; then
15 #  echo The directories to be compared must already exist.
16 #  exit 1
17 #fi
18 if [ "$dir1" == "$dir2" ]; then
19   echo "The two directories are the exact same folder name.  So that's silly."
20   exit 1
21 fi
22
23 out1="$(mktemp "$TMP/compare_dirs_output.XXXXXX")"
24 out2="$(mktemp "$TMP/compare_dirs_output.XXXXXX")"
25
26
27 #hmmm: need error checking in here!!!!
28
29
30 # host processing on first dir.
31 if [[ $dir1 == *":"* ]]; then
32   host1=${dir1%:*}
33   dir1=${dir1#*:}
34 #echo "got host1 as $host1 and new dir1 as $dir1"
35 fi
36
37 # host processing on second dir.
38 if [[ $dir2 == *":"* ]]; then
39   host2=${dir2%:*}
40   dir2=${dir2#*:}
41 #echo "got host2 as $host2 and new dir2 as $dir2"
42 fi
43
44 if [ -z "$host1" ]; then
45   # fully local compare location for first dir.
46   pushd "$dir1" &>/dev/null
47   find . >"$out1"
48   popd &>/dev/null
49 else
50   # remote compare location for first dir.
51   ssh "$host1" "cd \"$dir1\" && find ." >"$out1"
52 fi
53
54 # sort the output from listing the first directory.
55 sort "$out1" >"$out1".sort
56
57 if [ -z "$host2" ]; then
58   # fully local compare location for second dir.
59   pushd "$dir2" &>/dev/null
60   find . >"$out2"
61   popd &>/dev/null
62 else
63   # remote compare location for second dir.
64   ssh "$host2" "cd \"$dir2\" && find ." >"$out2"
65 fi
66
67 # sort the output from listing the second directory.
68 sort "$out2" >"$out2".sort
69
70 # compare the two sorted output files to show the missing files on each side.
71 diff "$out1".sort "$out2".sort
72
73 # clean up our output files.
74 rm "$out1" "$out1".sort "$out2" "$out2".sort
75