-
Notifications
You must be signed in to change notification settings - Fork 13
/
mv-merge
executable file
·98 lines (84 loc) · 2.22 KB
/
mv-merge
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
#!/bin/bash
#
# Moves the entire contents of one directory to another, even if some
# subdirectories are common to both source and destination directories.
#
# Uses rsync and rm-src-dups.
#
# Adam Spiers <me@adamspiers.org>
#
set -e
me=`basename $0`
usage () {
# Call as: usage [EXITCODE] [USAGE MESSAGE]
exit_code=1
if [[ "$1" == [0-9] ]]; then
exit_code="$1"
shift
fi
if [ -n "$1" ]; then
echo "$*" >&2
echo
fi
cat <<EOF >&2
Usage: $me [OPTIONS] SRC [SRC ...] DESTDIR
SRCs should be relative to the current directory.
DESTDIR can be relative or absolute.
Options (must be in order specified above!)
-f Force overwrites of existing files in dest tree
(does not overwrite by default)
-u Update existing files in dest tree only if source is newer
-n, --dry-run Don't actually do anything.
EOF
exit "$exit_code"
}
parse_opts () {
overwrite="--ignore-existing"
while [ $# != 0 ]; do
case "$1" in
-h|--help)
usage 0
;;
-n|--dry-run)
dry_run=--dry-run
shift
;;
-f)
overwrite=""
shift
;;
-u)
# takes precedence over -f
overwrite="--update"
shift
;;
-*)
usage "Unrecognized option $1"
;;
*)
break
;;
esac
done
if [ $# -lt 2 ]; then
usage "Need at least two parameters"
fi
srcs=( "${@:1:$(($#-1))}" )
dst="${@: -1:1}"
if ! [ -d "$dst" ]; then
usage "$dst is not a directory"
fi
}
if ! which rsync 2>&1 >/dev/null; then
echo "rsync must be on \$PATH for $me to work." >&2
exit 1
fi
parse_opts "$@"
abs_cwd=`pwd`
for src in "${srcs[@]}"; do
echo rsync -Rav --link-dest=\""$abs_cwd"\" $dry_run $overwrite \""$src"\" \""$dst"\"
rsync -Rav --link-dest="$abs_cwd" $dry_run $overwrite "$src" "$dst"
echo
echo rm-src-dups -vv $dry_run -s \""$src"\" . \""$dst"\"
[ -d "$dst/$src" ] && rm-src-dups -vv $dry_run -s "$src" . "$dst"
done