From e382dace970a7388a18cd758dab732fedbd2d8b5 Mon Sep 17 00:00:00 2001 From: Matteo Cypriani Date: Sun, 1 Jun 2014 20:38:55 -0400 Subject: [PATCH] [file_utils] mvparent: allow multiple arguments --- file_utils/mvparent.sh | 44 ++++++++++++++++++++++++++---------------- 1 file changed, 27 insertions(+), 17 deletions(-) diff --git a/file_utils/mvparent.sh b/file_utils/mvparent.sh index b3cda7b..8a9c06e 100755 --- a/file_utils/mvparent.sh +++ b/file_utils/mvparent.sh @@ -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