aboutsummaryrefslogtreecommitdiff
path: root/scripts/movefiles
blob: 2d93d403a715b05b3c65b35dd99c7212445915e5 (plain)
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
#!/bin/sh

set -e
set -u

src=''
dst=''

usage () {
cat << USAGE
Moves files and directories using GNU Tar.
Usage:
$0 -s sourcedir -d destdir files ...
USAGE
exit 1
}

fatal () {
    echo "$@" >&2
    echo "Type \`$0 -h' to get help." >&2
    exit 2
}

while getopts hd:s: opt; do
    case "$opt" in
        s) src="$OPTARG";;
        d) dst="$OPTARG";;
        *) usage;;
    esac
done
shift $OPTIND-1


if [ -z "$src" ]; then
    fatal "Missing source directory."
fi
if ! [ -e "$src" ]; then
    fatal "\`$src' does not exist."
fi
if ! [ -d "$src" ]; then
    fatal "\`$src' is not a directory."
fi
if [ -z "$dst" ]; then
    fatal "Missing destination directory."
fi
if ! [ -e "$dst" ]; then
    fatal "\`$dst' does not exist."
fi
if ! [ -d "$dst" ]; then
    fatal "\`$dst' is not a directory."
fi

src_n=`cd "$src" && pwd`
dst_n=`cd "$dst" && pwd`

if [ "$src_n" = "$dst_n" ]; then
    fatal "\`$src' and \`$dst' are the same."
fi

list="/tmp/cibs-movelist.$$"

rm -f "$list"

for f in "$@"; do
    ( set -e; set -x; cd "$src_n" && gfind $f ! -type d -print ) >> "$list"
done

( set -e; set -x; cd "$src_n" && gtar cf - -T "$list" ) | \
    ( set -e; set -x; cd "$dst_n" && gtar xf - )

(set -e; set -x; cd "$src_n" && cat "$list" | xargs rm -f )

exit 0