Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?

Usage of the rsync utility in Linux

Some useful constructs that make rsync powerful

Accessing the help manual

man rsync


Common constructs used with rsync

Syncing the contents from one directory to another recursively

rsync -r dir1/ dir2

Syncing recursively and maintaining source file metadata in destination

rsync -a dir1/ dir2

Testing rsync with a dry run

rsync -an dir1/ dir2

Add the verbose flag to make the command effective by displaying the files/directories that will be synced
rsync -anv dir1/ dir2

Pushing files from local machine to a remote server

rsync -a dir1/ username@host:destination_directory_on_server

Pulling files from a remote server to a local machine

rsync -a username@host:source_directory_on_server directory_on_local_machine

Compress files during transfer

rsync -az source destination

Adding a progress bar and syncing only changed files

rsync -aP dir1/ dir2

Delete file from the destination directory if it was removed from the source directory

rsync -a --delete dir1/ dir2

Excluding files or directories during sync

rsync -a --exclude=pattern_to_exclude dir1/ dir2

Overriding exclusion for files that include a specific pattern

rsync -a --exclude=pattern_to_exclude --include=pattern_to_include dir1/ dir2

Creating backups

rsync -a --delete --backup --backup-dir=~/backup ~/dir1/ ~/dir2

The above command will:

  1. Look for any files changed in dir1 or dir2 and move the previous version of the file to the backup folder
  2. Look for files deleted in dir1 and move the corresponding file from dir2 to the backup folder before deleting it from dir2

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.