ambevar-dotfiles/.scripts/mover

76 lines
1.7 KiB
Plaintext
Raw Normal View History

#!/bin/sh
if [ "$1" = "-h" ] || [ $# -lt 2 ]; then
cat <<EOF
${0##*/} FOLDERS DEST
2013-12-27 11:16:17 +01:00
Merge FOLDERS content into DEST. Existing files and folders will remain. It does
not overwrite by default. Empty folders are ignored. The resulting hierarchy is:
DEST/FOLDER1/<folder1data>
DEST/FOLDER2/<folder1data>
...
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
2013-12-27 11:16:17 +01:00
## We the counter we offer the interesting feature of processing all arguments
## but the last one. We can get the last argument with an 'eval'. Note that the
## 'eval' is safe here.
DEST="$(eval "echo \$$#")"
count=0
for i ; do
count=$(($count+1))
[ $count -eq $# ] && break
while IFS= read -r FILE; do
DESTFILE="$DEST/$FILE"
if [ ! -e "$DESTFILE" ] || $OPT_OVERWRITE; then
mkdir -p "$(dirname "$DESTFILE")"
2013-12-27 11:16:17 +01:00
mv -v "$i/../$FILE" "$DESTFILE"
if $OPT_DELEMPTY; then
PARENT="$FOLDER/../$FILE"
PARENT="${PARENT%/*}"
rmdir "$PARENT" 2>/dev/null
fi
fi
2013-12-27 11:16:17 +01:00
## We switch to $i so that 'find' strips the parent dirs from the path.
done <<EOF
2013-12-27 11:16:17 +01:00
$(cd "$i/.." && find "$(basename ${i})" \( -type f -o -type l \) )
EOF
2013-12-27 11:16:17 +01:00
done