Skip to content

Instantly share code, notes, and snippets.

@KevinWang15
Created August 9, 2024 13:32
Show Gist options
  • Save KevinWang15/fc1c08900cdb6cd03f560750f518ba9b to your computer and use it in GitHub Desktop.
Save KevinWang15/fc1c08900cdb6cd03f560750f518ba9b to your computer and use it in GitHub Desktop.
multi-rsync
#!/bin/bash
# Check if both parameters are provided
if [ $# -ne 2 ]; then
echo "Usage: $0 <target_directory> <comma_separated_word_list>"
exit 1
fi
target_dir="$1"
word_list=$(echo "$2" | tr ',' ' ')
# Check if target directory exists
if [ ! -d "$target_dir" ]; then
echo "Target directory '$target_dir' does not exist. Create it? (y/n): "
read -r create_dir
if [ "$create_dir" = "y" ] || [ "$create_dir" = "Y" ]; then
mkdir -p "$target_dir"
else
echo "Target directory does not exist. Exiting."
exit 1
fi
fi
# Find directories containing any of the words
matching_dirs=$(find . -maxdepth 1 -type d | grep -E $(echo $word_list | sed 's/ /|/g'))
# Print matching directories for confirmation
echo "Matching directories:"
echo "$matching_dirs"
# Ask for confirmation
read -p "Do you want to proceed with rsync? (y/n): " confirm
if [ "$confirm" = "y" ] || [ "$confirm" = "Y" ]; then
# Perform rsync for each matching directory
echo "$matching_dirs" | while read -r dir; do
if [ -n "$dir" ]; then
echo "Syncing $dir to $target_dir"
rsync -avW --progress "$dir" "$target_dir"
fi
done
else
echo "Operation cancelled."
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment