Merge branch 'release-2.140.93'
[feisty_meow.git] / scripts / files / remove_here_if_not_there.sh
1 #!/bin/bash
2
3 function print_instructions()
4 {
5   echo -e "\n$(basename $0 .sh):\n"
6   echo -e 'this script takes two parameters, a "here" folder and a "there" folder, almost as if it were a copy command.  but instead, this removes any file from under the "here" location if it cannot be found in the "there" location.  so the "there" location is considered a more definitive template of what should be in "here", such that we strip out what "there" does not have.\n\n
7 the most" useful way to use this script is for a "here" hierarchy that is a copy of an older version of another "there" hierarchy.  the "there" hierarchy may have changed a lot, including new files, changed files, and deleted files.  it is a simple operation to copy everything from "there" into "here" (such as by using the command [ cp -R "$there"/* "$here" ] ) , but it is a lot harder to determine what stuff in "here" is out of date and should be removed.  that is where this script comes in; it can be run to flush out any older things in "here", rather than requiring the user to manually find all those files.  ' | splitter
8   echo
9   echo "Example Usage:"
10   echo 
11   echo "$(basename $0 .sh) backup_copy original_folder"
12   echo
13   echo " ==> Deletes any files in backup_copy that are not found in original_folder." 
14   echo 
15 }
16
17 if [ $# -lt 2 ]; then
18   print_instructions
19   echo "There were not enough parameters provided to the script."
20   exit 1
21 fi
22
23 here="$1"; shift
24 there="$1"; shift
25
26 if [ ! -d "$there" -o ! -d "$here" ]; then
27   print_instructions
28   echo "One of the directories specified does not exist."
29   exit 1
30 fi
31
32 here_temp_file="$(mktemp "$TMP/remover_list.XXXXXX")"
33 there_temp_file="$(mktemp "$TMP/remover_list.XXXXXX")"
34
35 # find all the files in both hierarchies.
36 pushd "$here" &>/dev/null
37 find "." -type f | sort >"$here_temp_file"
38 popd &>/dev/null
39 pushd "$there" &>/dev/null
40 find "." -type f | sort >"$there_temp_file"
41 popd &>/dev/null
42
43 whack_list="$(mktemp "$TMP/remover_list.XXXXXX")"
44
45 comm -23 "$here_temp_file" "$there_temp_file" >"$whack_list"
46
47 while read input_text; do
48   if [ -z "$input_text" ]; then break; fi
49   herename="$here/$input_text"
50   rm -v "$herename"
51 done <"$whack_list"
52
53 # clean up.
54 rm "$whack_list" "$here_temp_file" "$there_temp_file"
55