Rsync (Remote Sync) is a useful command-line utility for Unix-like systems to synchronize files between two remote systems. A user can also use the Rsync command to sync files between two directories on the same system. The Rsync uses a delta-transfer algorithm, which sends only the differences between the source files and the existing files in the destination. It also reduced the data on the network to save bandwidth.
The Rsync is generally used for backup purposes or for transferring a large number of files between two systems. Rsync support local to local, local to remote, and remote to local syncing of files. But it doesn’t support remote to remote file syncing.
In this tutorial, we will discuss Rsync command-line tool with multiple frequently uses examples.
rsync [OPTION...] SOURCE... [DESTINATION]
- Here SOURCE can be a local directory like “/opt/backup” or a remote location like “[email protected]:/opt/remotedir/”
- Also the DESTINATION can be refer to a local directory or remote system directory
- Both SOURCE and DESTINATION can refer to a local directory .
- Only one SOURCE or DESTINATION can refer to the remote location . You can’t use both as a remote location.
Rsync Command Options
Rsync offers a large number of useful options that control each and every aspect of its behavior and make it more flexible for synchronizing files. Below is the list of some frequently used Rsync command options:
-v, -vv -vvv, --verbose– This option is used for verbose logs on standard output. The default rsync runs silently. Higher the number of “v” increases logging level.
-a, --archive– Is the most common used option with rsync command. The archive mode is equivalent to the options
-rlptgoD. Basically, it includes all necessary options like, recursively, preserve file permissions, symbolic links, file ownership and the timestamps.
-z, -–compress– Use this option to transfer data in compressed format. Which is useful to save bandwidth.
-h, –-human-readable– Use this option to print all the outputs in a human readable format.
--delete– Use this option only if you need to remove files on destination, which does not exists on source.
--exclude=PATTERN– Exclude files from rsync matching the given pattern.
rsync --exclude '*.jpg' SRC/ DEST/
--include=PATTERN– Include files in rsync matching the given pattern.
rsync --include '*.jpg' --include '*.txt' --exclude '*' SRC/ DEST/
Rsync – Useful Command Line Examples
Here are 12 practical examples of the Rsync command in Unix/Linux systems.
- Sync (Rsync) files locally: You can also use rsync command to sync files between two local directories. For example, the following command will copy all content from
dest/directory. The command will automatically create destination if not exists.
rsync -avh src/ dest/
- Rsync files from local to remote: The Rsync allows to sync of files between local and remote systems. You can keep one from the source or destination as a remote host.
rsync -avh src/ [email protected]:/dest/
- Rsync files from remote to local: Use the source directory from the remote host and the destination must be the local directory.
rsync -avh [email protected]:/src/ /dest/
- Using Rsync over SSH: The Rsync connects to remote systems using the Rsync daemon directly via TCP. Also, we can specify to use Secure Shell (SSH) to communicate with remote systems.
-eoption to specify the remote shell. The below command will use ssh as remote shell:
rsync -avhze ssh src/ [email protected]:/dest/
If the SSH server on destination is running on non-standard port use this tutorial to connect rsync over non-standard port.
- Ignore existing files with Rsync: Use the Rsync command with –ignore-existing option to ignore all files, which is already exists on the destination.
rsync --ignore-existing -avh src/ dest/
- Update file only if the source is newer: Use
--updateoption to update file on remote only if there is a newer version is local. The Rsync will create a file on the destination if not exists. Also update file on the destination of a local timestamp is newer than the remote system file.
rsync --update -avh src/ [email protected]:/dest/
- Remove file from source with Rsync: Use option
--remove-source-filesto remote file from source after successful file transfer to destination.
rsync --remove-source-files -avh /src [email protected]:/dest
- Exclude specific files with Rsync: You can specify a file or pattern with
--exclude=PATTERNoption to exclude from rsync.
rsync -avh --exclude '*.zip' --exclude '*.log' /src [email protected]:/dest
- Include specific files with Rsync: The default Rsync includes all files under the source directory tree. Now, you need to include only specific files to synchronize to the destination.
Here you can specify
--include=PATTERNto include specific files and exclude all files with
rsync -avh -include '*.jpg' --include '*.txt' --exclude '*' /src [email protected]:/dest
- Display Progress with Rsync: Use
--progressoption to display progress during the file transfer between source and destination with Rsync command.
rsync -avh --progress /src [email protected]:/dest
- Rsync with changing file permission:
You can instruct the Rsync to change files owner and group owners on destination. Use option
--chown=USER:GROUPwith Rsync to change file permission.
rsync -avh --chown=USER:GROUP /src [email protected]:/dest
Similarly, you can use the –chmod=CHMOD option to change file or directory permissions on the destination.
rsync -avh --chmod=755 /src [email protected]:/dest
- Rsync dry-run only: Use
--dry-runoption to execute dry run only. It will show you similar results as the original command but nothing will update on the destination. Basically, we can use this to check, what the Rsync will update before actually running it.
rsync -avh --dry-run /src [email protected]:/dest
Rsync is a very useful utility for transferring small to large amounts of data between two systems. You can also use Rsync to backup the entire system at a remote backup drive. In this tutorial, you have learned a few useful Rsync command examples that are really helpful for daily tasks.