diff --git a/file_utils/mvparent.sh b/file_utils/mvparent.sh index 79c64c4..951d9be 100755 --- a/file_utils/mvparent.sh +++ b/file_utils/mvparent.sh @@ -8,43 +8,47 @@ # To Public License, Version 2, as published by Sam Hocevar. See # http://sam.zoy.org/wtfpl/COPYING for more details. # -# Moves the contents of the given directory into its parent directory. +# Moves the contents of the given directory(ies) into its parent directory, +# then removes the directory(ies). + +set -e +#set -x if [ $# -lt 1 ] ; then - echo "Usage: `basename $0` DIRECTORY [DIRECTORY [...]]" >&2 + echo "Usage: $(basename "$0") DIRECTORY [DIRECTORY [...]]" >&2 exit 1 fi -while [ "$1" != "" ] ; do - REP="$1" +while [ -n "$1" ] ; do + dir="$1" shift - if [ ! -d "$REP" ] ; then - echo "Warning! \"$REP\" is not a directory: ignoring." >&2 + if [ ! -d "$dir" ] ; then + echo "Warning! \"$dir\" is not a directory: ignoring." >&2 continue fi - TARGET=$(basename "$REP") - REP=$dirname "$REP") + target="$(basename "$dir")" + dir="$(dirname "$dir")" - if [ "$TARGET" = "." -o "$TARGET" = ".." ] ; then + if [ "$target" = "." ] || [ "$target" = ".." ] ; then echo "Target cannot end by « . » or « .. »!" >&2 exit 2 fi - echo "Moving the contents of « $REP/$TARGET » into « $REP »..." >&2 + echo "Moving the contents of « $dir/$target » into « $dir »..." >&2 - cd "$REP" + cd "$dir" - if [ "$(echo "$TARGET"/*)" != "$TARGET/*" ] ; then - mv -n "$TARGET"/* . + if [ "$(echo "$target"/*)" != "$target/*" ] ; then + mv -n "$target"/* . fi - if [ "$(echo "$TARGET"/.[!.]*)" != "$TARGET/.[!.]*" ] ; then - mv -n "$TARGET"/.[!.]* . + if [ "$(echo "$target"/.[!.]*)" != "$target/.[!.]*" ] ; then + mv -n "$target"/.[!.]* . fi - rmdir "$TARGET" + rmdir "$target" done # vim: tabstop=8 expandtab shiftwidth=4 softtabstop=4