2013-09-03 14:42:46 +02:00
|
|
|
#!/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>
|
|
|
|
...
|
2013-09-03 14:42:46 +02:00
|
|
|
|
|
|
|
Options:
|
2013-10-11 14:31:34 +02:00
|
|
|
-f: Overwrite destination.
|
|
|
|
-r: Remove empty folders.
|
2013-09-03 14:42:46 +02:00
|
|
|
EOF
|
|
|
|
exit
|
|
|
|
fi
|
|
|
|
|
|
|
|
OPT_OVERWRITE=false
|
2013-10-11 14:31:34 +02:00
|
|
|
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
|
2013-09-03 14:42:46 +02:00
|
|
|
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
|
2013-09-03 14:42:46 +02:00
|
|
|
|
2013-10-23 17:44:23 +02:00
|
|
|
while IFS= read -r FILE; do
|
2013-09-03 14:42:46 +02:00
|
|
|
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"
|
2013-10-11 14:31:34 +02:00
|
|
|
|
|
|
|
if $OPT_DELEMPTY; then
|
|
|
|
PARENT="$FOLDER/../$FILE"
|
|
|
|
PARENT="${PARENT%/*}"
|
|
|
|
rmdir "$PARENT" 2>/dev/null
|
|
|
|
fi
|
2013-09-03 14:42:46 +02:00
|
|
|
fi
|
|
|
|
|
2013-12-27 11:16:17 +01:00
|
|
|
## We switch to $i so that 'find' strips the parent dirs from the path.
|
2013-09-03 14:42:46 +02:00
|
|
|
done <<EOF
|
2013-12-27 11:16:17 +01:00
|
|
|
$(cd "$i/.." && find "$(basename ${i})" \( -type f -o -type l \) )
|
2013-10-23 17:44:23 +02:00
|
|
|
EOF
|
2013-12-27 11:16:17 +01:00
|
|
|
done
|