Skip to content

Instantly share code, notes, and snippets.

@markbosky
Created August 9, 2024 17:23
Show Gist options
  • Save markbosky/3a2ffd3475eac64b006cb0afe2d967ed to your computer and use it in GitHub Desktop.
Save markbosky/3a2ffd3475eac64b006cb0afe2d967ed to your computer and use it in GitHub Desktop.
RSync shell script to copy files from one directory to another while maintaining dir structure
#!/bin/bash
# List of files to copy
files=(
"/path/to/source/folder1/file1.txt"
"/path/to/source/folder2/file2.txt"
"/path/to/source/folder3/file3.txt"
)
# Source and destination directories
source_dir="/path/to/source"
dest_dir="/path/to/destination"
# Copy files while maintaining directory structure
for file in "${files[@]}"; do
# Get the relative path of the file
relative_path="${file#$source_dir/}"
# Get the directory of the file
file_dir=$(dirname "$relative_path")
# Create the directory structure in the destination
mkdir -p "$dest_dir/$file_dir"
# Copy the file to the destination
cp "$file" "$dest_dir/$relative_path"
done
echo "Files have been copied successfully."
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment