scripts/file_utils/mvparent.sh

51 lines
1.3 KiB
Bash
Raw Normal View History

#!/bin/sh
#
# mvparent.sh, Copyright © 2007, 2010 Matteo Cypriani
#
# This program is free software. It comes without any warranty, to
# the extent permitted by applicable law. You can redistribute it
# and/or modify it under the terms of the Do What The Fuck You Want
# 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.
if [ $# -lt 1 ] ; then
2018-04-11 20:59:48 +02:00
echo "Usage: `basename $0` DIRECTORY [DIRECTORY [...]]" >&2
exit 1
fi
while [ "$1" != "" ] ; do
2018-04-11 20:59:48 +02:00
REP="$1"
shift
2018-04-11 20:59:48 +02:00
if [ ! -d "$REP" ] ; then
echo "Warning! \"$REP\" is not a directory: ignoring." >&2
continue
fi
2018-04-11 20:59:48 +02:00
TARGET=$(basename "$REP")
REP=$dirname "$REP")
2018-04-11 20:59:48 +02:00
if [ "$TARGET" = "." -o "$TARGET" = ".." ] ; then
echo "Target cannot end by « . » or « .. »!" >&2
exit 2
fi
2018-04-11 20:59:48 +02:00
echo "Moving the contents of « $REP/$TARGET » into « $REP »..." >&2
2018-04-11 20:59:48 +02:00
cd "$REP"
2018-04-11 20:59:48 +02:00
if [ "$(echo "$TARGET"/*)" != "$TARGET/*" ] ; then
mv -n "$TARGET"/* .
fi
2018-04-11 20:59:48 +02:00
if [ "$(echo "$TARGET"/.[!.]*)" != "$TARGET/.[!.]*" ] ; then
mv -n "$TARGET"/.[!.]* .
fi
2018-04-11 20:59:48 +02:00
rmdir "$TARGET"
done
2018-04-11 20:59:48 +02:00
# vim: tabstop=8 expandtab shiftwidth=4 softtabstop=4