Bash - Rsync - Copy and sync files

Copy

rsync -az $source $target

Copy over SSH:

rsync -az $source [email protected]:$target

Trailing slash or not

Trailing slash on destination directory has no effect

Copy “hello.txt” file inside “bar” directory resulting in “bar/hello.txt”:

rsync -az hello.txt bar

Copy “foo” directory inside “bar” directory resulting in “bar/foo”:

rsync -az foo bar

Copy files of “foo/*” directory inside “bar” directory resulting in “bar/*”:

rsync -az foo/ bar

Sync

Remove files in destination not in source:

rsync -az --delete foo/ bar

Exclude files

rsync -az --delete \
  --exclude ".git" \
  --exclude ".gitignore" \
  foo/ \
  bar