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
-vto see what will change before performing a real Rsync.
- -P : show the progress of the transfer. Same as
- -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)
rsync -aP '/origin/' '/destination'
Note that options can be joined together without the additional
each one. For example, to use archive, verbose and Progress
rsync -avP '/origin/' '/destination'
Exclude files using Rsync
To exclude a specific file, use the
rsync -avP --exclude 'cats.jpg' "/origin/" "/destination"
To exclude more than one file, add another
rsync -avP --exclude 'cats.jpg' --exclude 'dogs.jpg' "/origin/" "/destination"
To exclude all files of the same type, e.g. all
rsync -avP --exclude '*.jpg' "/origin/" "/destination"
Ignore existing files at destination with Rsync
--ignore-existing option to ignore all existing files in the
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"
--delete will proceed without asking for confirmation. It is
recommended to perform a dry-run when using this option by specifying
rsync -n -avP --delete "/origin/" "/destination" #or rsync --dry-run -avP --delete "/origin/" "/destination"