[file_utils] mvparent: allow multiple arguments

This commit is contained in:
Matteo Cypriani 2014-06-01 20:38:55 -04:00
parent 83e5bfddeb
commit e382dace97
1 changed files with 27 additions and 17 deletions

View File

@ -10,29 +10,39 @@
#
# Moves the contents of the given directory into its parent directory.
if [ $# -ne 1 -o ! -d "$1" ] ; then
echo "Usage: `basename $0` DIRECTORY" >&2
if [ $# -lt 1 ] ; then
echo "Usage: `basename $0` DIRECTORY [DIRECTORY [...]]" >&2
exit 1
fi
rep=$(dirname "$1")
cible=$(basename "$1")
while [ "$1" != "" ] ; do
REP="$1"
shift
if [ "$cible" = "." -o "$cible" = ".." ] ; then
echo "Target cannot end by « . » or « .. »!" >&2
exit 2
fi
if [ ! -d "$REP" ] ; then
echo "Warning! \"$REP\" is not a directory: ignoring." >&2
continue
fi
echo "Move the contents of « $rep/$cible » into « $rep »…" >&2
TARGET=$(basename "$REP")
REP=$(dirname "$REP")
cd "$rep"
if [ "$TARGET" = "." -o "$TARGET" = ".." ] ; then
echo "Target cannot end by « . » or « .. »!" >&2
exit 2
fi
if [ "$(echo "$cible"/*)" != "$cible/*" ] ; then
mv "$cible"/* .
fi
echo "Moving the contents of « $REP/$TARGET » into « $REP »..." >&2
if [ "$(echo "$cible"/.[!.]*)" != "$cible/.[!.]*" ] ; then
mv "$cible"/.[!.]* .
fi
cd "$REP"
rmdir "$cible"
if [ "$(echo "$TARGET"/*)" != "$TARGET/*" ] ; then
mv "$TARGET"/* .
fi
if [ "$(echo "$TARGET"/.[!.]*)" != "$TARGET/.[!.]*" ] ; then
mv "$TARGET"/.[!.]* .
fi
rmdir "$TARGET"
done