ambevar-dotfiles/.scripts/mover

70 lines
1.4 KiB
Bash
Executable File

#!/bin/sh
if [ "$1" = "-h" ] || [ $# -lt 2 ]; then
cat <<EOF
${0##*/} FOLDERS DEST
Move FOLDERS into DEST. Does not overwrite by default. Empty folders are ignored.
Options:
-f: Overwrite destination.
-r: Remove empty folders.
EOF
exit
fi
OPT_OVERWRITE=false
OPT_DELEMPTY=false
while getopts ":fhr" opt; do
case $opt in
h)
_printhelp "$0"
exit 1 ;;
f)
OPT_OVERWRITE=true ;;
r)
OPT_DELEMPTY=true ;;
?)
_printhelp "$0"
exit 1 ;;
:)
_printhelp "$0"
exit 1 ;;
esac
done
shift $(($OPTIND - 1))
if [ $# -eq 0 ]; then
_printhelp "$0"
exit 1
fi
for i; do
ARGLIST="${ARGLIST:+$ARGLIST\n}$i"
done
FOLDERS="$(echo $ARGLIST | head -n-1)"
DEST="$(echo $ARGLIST | tail -n1)"
while IFS= read -r FOLDER; do
while IFS= read -r FILE; do
DESTFILE="$DEST/$FILE"
if [ ! -e "$DESTFILE" ] || $OPT_OVERWRITE; then
mkdir -p "$(dirname "$DESTFILE")"
mv -v "$FOLDER/../$FILE" "$DESTFILE"
if $OPT_DELEMPTY; then
PARENT="$FOLDER/../$FILE"
PARENT="${PARENT%/*}"
rmdir "$PARENT" 2>/dev/null
fi
fi
## We switch to FOLDER so that 'find' strips the parent dirs from the path.
done <<EOF
$(cd "$FOLDER/.." && find "$(basename ${FOLDER})" \( -type f -o -type l \) )
EOF
done <<EOF
$(echo "$FOLDERS")
EOF