Skip to content

Instantly share code, notes, and snippets.

@mrjk
Created December 14, 2022 09:27
Show Gist options
  • Save mrjk/d82c4d6dd0948bbb6fcef8051e640375 to your computer and use it in GitHub Desktop.
Save mrjk/d82c4d6dd0948bbb6fcef8051e640375 to your computer and use it in GitHub Desktop.
Merge two directory and does not overwrite things.
#!/bin/bash
# To install in in: /usr/local/bin/merge_dirs
# Then: chmod +x /usr/local/bin/merge_dirs
set -eu
# Source: https://unix.stackexchange.com/a/155633
# Usage: like mv but: SRC DEST
DEST="${@:${#@}}"
ABS_DEST="$(cd "$(dirname "$DEST")"; pwd)/$(basename "$DEST")"
OVERRIDE=false
for SRC in "${@:1:$((${#@} -1))}"; do (
cd "$SRC";
echo "cd $SRC"
find . -type d -exec mkdir -p "${ABS_DEST}"/\{} \;
# We do not override !
find . -type f -exec mv -n \{} "${ABS_DEST}"/\{} \;
find . -type d -empty -delete
) done
rmdir "$SRC"
if [[ -d "$SRC" ]]; then
tree "$SRC"
echo "Success: Directory $SRC had un-moved duplicates"
echo "Run: diff -r -d '$SRC' '$DEST' | not grep -v '^Only in $DEST'"
echo "If you are sure to delete source: rm -rf '$SRC'"
else
echo "Success: Directory $SRC has been merged into $ABS_DEST"
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment