Skip to content

Instantly share code, notes, and snippets.

@ryancraigmartin
Created November 13, 2024 00:55
Show Gist options
  • Save ryancraigmartin/f709f28d2f68902bbee10268dbad7cbc to your computer and use it in GitHub Desktop.
Save ryancraigmartin/f709f28d2f68902bbee10268dbad7cbc to your computer and use it in GitHub Desktop.
#!/bin/bash
if [ $# -ne 2 ]; then
echo "Usage: $0 <source_directory> <destination_directory>"
exit 1
fi
source_dir="$1"
dest_dir="$2"
if [ ! -d "$source_dir" ] || [ ! -d "$dest_dir" ]; then
echo "Both source and destination must be valid directories"
exit 1
fi
# Get the number of CPU cores
num_cores=$(sysctl -n hw.ncpu)
# Calculate the number of parallel processes (1.5 times the number of cores)
num_processes=$((num_cores * 3 / 2))
# First, mirror the directory structure
rsync -a --include='*/' --exclude='*' "$source_dir/" "$dest_dir/"
# Then, move files in parallel
cd "$source_dir" && find . -type f -print0 | \
xargs -0 -n1 -P$num_processes -I{} \
rsync -avz --remove-source-files --relative {} "$dest_dir/"
echo "Finished moving files."
# Remove empty directories in the source
find "$source_dir" -type d -empty -delete
echo "Removed empty directories from source."
# Remove the source directory
if [ -d "$source_dir" ]; then
rm -rf "$source_dir"
echo "Removed source directory: $source_dir"
else
echo "Source directory no longer exists: $source_dir"
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment