Skip to content

Instantly share code, notes, and snippets.

@bumbummen99
Last active December 20, 2024 23:03
Show Gist options
  • Save bumbummen99/e672540d74c1a2ed429766917f85eab0 to your computer and use it in GitHub Desktop.
Save bumbummen99/e672540d74c1a2ed429766917f85eab0 to your computer and use it in GitHub Desktop.
rsync based mv alternative handling recursive operation and duplicates.
#!/usr/bin/env bash
# This script is a wrapper around rsync and is intended to work like mv. Unlike
# mv this script does move data recursively and prevent duplicates by appending
# a suffix. The script will create hardlinks to the actual filesystem data in
# the destination but NOT delete the old hardlinks from source.
#
# Author: Patrick Henninger <[email protected]>
# License: GPL v3 (https://choosealicense.com/licenses/gpl-3.0)
# Ensure arguments have been provided
if [ "$#" -ne 2 ]; then
echo "Usage: $0 <source> <destination>"
exit 1
fi
SOURCE="$1"
DESTINATION="$2"
# Start building the rsync command
CMD=(rsync)
#################
# Functionality #
#################
# Don't cross filesystem boundaries (mv can not do this either)
CMD+=(--one-file-system)
# Hardlink to files in SOURCE when unchanged (since it is the same filesystem)
CMD+=(--link-dest="$(dirname "$(realpath "$SOURCE")")")
# Backup duplicates in the destination
CMD+=(--backup)
# Work recursively and preserve everything
CMD+=(--archive)
############
# Cosmetic #
############
# Show progress during transfer
CMD+=(--progress)
# Execute the rsync command with the provided arguments
"${CMD[@]}" "$SOURCE" "$DESTINATION" "$@"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment