scripts/file_utils/mvparent.sh

56 lines
1.4 KiB
Bash
Raw Normal View History

#!/bin/sh
#
# mvparent.sh, Copyright © 2007, 2010, 2020 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.
#
2018-04-11 21:09:21 +02:00
# 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
2018-04-11 21:09:21 +02:00
echo "Usage: $(basename "$0") DIRECTORY [DIRECTORY [...]]" >&2
2018-04-11 20:59:48 +02:00
exit 1
fi
2018-04-11 21:09:21 +02:00
while [ -n "$1" ] ; do
dir="$1"
2018-04-11 20:59:48 +02:00
shift
2018-04-11 21:09:21 +02:00
if [ ! -d "$dir" ] ; then
echo "Warning! \"$dir\" is not a directory: ignoring." >&2
2018-04-11 20:59:48 +02:00
continue
fi
2018-04-11 21:09:21 +02:00
target="$(basename "$dir")"
dir="$(dirname "$dir")"
2018-04-11 21:09:21 +02:00
if [ "$target" = "." ] || [ "$target" = ".." ] ; then
2018-04-11 20:59:48 +02:00
echo "Target cannot end by « . » or « .. »!" >&2
exit 2
fi
2018-04-11 21:09:21 +02:00
echo "Moving the contents of « $dir/$target » into « $dir »..." >&2
2018-04-11 21:09:21 +02:00
cd "$dir"
2018-04-11 21:09:21 +02:00
if [ "$(echo "$target"/*)" != "$target/*" ] ; then
mv -n "$target"/* .
2018-04-11 20:59:48 +02:00
fi
2018-04-11 21:09:21 +02:00
if [ "$(echo "$target"/.[!.]*)" != "$target/.[!.]*" ] ; then
mv -n "$target"/.[!.]* .
2018-04-11 20:59:48 +02:00
fi
2018-04-11 21:09:21 +02:00
rmdir "$target"
cd "$OLDPWD"
done
2018-04-11 20:59:48 +02:00
# vim: tabstop=8 expandtab shiftwidth=4 softtabstop=4