aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorIgor Pashev <igor.pashev@nexenta.com>2013-01-18 19:08:38 +0400
committerIgor Pashev <igor.pashev@nexenta.com>2013-01-18 19:08:38 +0400
commit161619e0ba85f0b9b6d6a51c9f386018660cce2c (patch)
tree1c63e0b7f710e89f2476c2eab612d8940b8da642
parent902901f8a0d4b4a83c436bc350f936d129fcda7c (diff)
downloadcibs-161619e0ba85f0b9b6d6a51c9f386018660cce2c.tar.gz
Script to move files
-rwxr-xr-xscripts/movefiles74
1 files changed, 74 insertions, 0 deletions
diff --git a/scripts/movefiles b/scripts/movefiles
new file mode 100755
index 0000000..74d91d9
--- /dev/null
+++ b/scripts/movefiles
@@ -0,0 +1,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 -x; cd "$src_n" && gfind $f ! -type d -print || true ) >> "$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
+