Rsync Basics Tutorial With Examples

 Tue, 01 Nov 2022 11:50 UTC

Rsync Basics Tutorial With Examples
Image: CC BY 4.0 by cybrkyd


Rsync is a tool used to synchronise files both locally and remotely on Linux systems. Commonly used for backups and mirroring, Rsync uses a delta-transfer algorithm which sends only the differences between the files.

Some common options for Rsync commands

  • -a : archive mode. Copies files recursively and will also preserve all file permissions and timestamps.
  • -n : dry-run. Rsync performs a trial run without making any changes. Commonly used with -v to see what will change before performing a real Rsync.
  • -P : show the progress of the transfer. Same as --progress.
  • -v : verbose. This option increases the amount of information you are given during the transfer.
  • --delete : delete files at the destination which do not exist at the source.
  • --exclude : used to specify files to exclude from the transfer.
  • --ignore-existing : skip existing files at destination. Does not affect directories.

Basic Rsync operation

To perform a copy from one location to another:

$ rsync -a '/origin/' '/destination'

Note the forward-slash at the end of the /origin/ directory name. If only transferring one file, do not add the trailing slash. For example:

$ rsync -a 'file.txt' /tmp/

In all cases, the destination directory can be specified with or without the trailing slash.

Show progress of Rsync

To show the progress of the transfer, use the (uppercase) -P option:

$ rsync -aP '/origin/' '/destination'

Note that options can be joined together without the additional - before each one. For example, to use archive, verbose and Progress together:

$ rsync -avP '/origin/' '/destination'

Exclude files using Rsync

To exclude a specific file, use the --exclude option:

$ rsync -avP --exclude 'cats.jpg' "/origin/" "/destination"

To exclude more than one file, add another --exclude option:

$ rsync -avP --exclude 'cats.jpg' --exclude 'dogs.jpg' "/origin/" "/destination"

To exclude all files of the same type, e.g. all .jpg files:

$ rsync -avP --exclude '*.jpg' "/origin/" "/destination"

Ignore existing files at destination with Rsync

Use the --ignore-existing option to ignore all existing files in the destination directory:

$ rsync -avP --ignore-existing "/origin/" "/destination"

Note that this option does not affect directories. This is a useful option for backups, for example, where a source folder contains some older files that have already been synchronised.

Delete at target

Use this option to delete files at the destination that do not exist at the origin.

$ rsync -avP --delete "/origin/" "/destination"

Note that --delete will proceed without asking for confirmation. It is recommended to perform a dry-run when using this option by specifying -n or --dry-run.

$ rsync -n -avP --delete "/origin/" "/destination"

#or

rsync --dry-run -avP --delete "/origin/" "/destination"