Skip to content

Instantly share code, notes, and snippets.

@Hakier
Created January 16, 2023 07:21
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Hakier/a585de0fd9bc223febab4ec64517568b to your computer and use it in GitHub Desktop.
Save Hakier/a585de0fd9bc223febab4ec64517568b to your computer and use it in GitHub Desktop.
Rsync: backup only specified files and preserve relative paths without a parent directory
#!/usr/bin/env bash
#setup
mkdir -p playground/{source,target} && cd playground
mkdir -p source/{a,b,c}/{d,e,f}/{g,h,i}
touch source/{.,a,b/{e,f},c/d/{g,h,i}}/{1,2,3,4,5}
#first sync
cd source && rsync -aR 3 a/ b/f/ c/d ../target/ --delete && cd -
find -type f
#remove a file
echo -n 'Remove a file (source/b/f/3) to check if --delete will work as expected [enter]: '; read cnt
rm source/b/f/3
#second sync
cd source && rsync -aR 3 a/ b/f/ c/d ../target/ --delete && cd -
find -type f
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment