Rsync (Remote Sync) is an useful command line utility for Unix like systems to synchronize files between two remote systems. A user can also use rsync command to sync files between two directory on same system. The rsync uses 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 network to save bandwidth.
The Rsync is generally used for the backup purpose or transferring 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 files syncing.
In this tutorial you will learn rsync command line tools with examples.
Syntax
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 controls 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 Command Examples
Here is some useful command line example of rsync command on Unix/Linux systems.
1. Rsync/Copy 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 src/
directory to dest/
directory. The command will automatically create destination if not exists.
rsync -avh src/ dest/
2. Rsync files from local to remote
The Rsync allows to sync files between local and remote systems. You can keep one from source or destination as remote host.
rsync -avh src/ [email protected]:/dest/
3. Rsync files from remote to local
Use the source directory from remote host and the destination must be the local directory.
rsync -avh [email protected]:/src/ /dest/
4. Using Rsync over SSH
The Rsync connects remote system using the rsync daemon directly via TCP. Also we can specify to use Secure Shell (SSH) to communicate with remote systems.
Use -e
option to specify the remote shell. The below command will use ssh as remote shell:
rsync -avhze ssh src/ [email protected]:/dest/
The SSH server running on non-standard port use this tutorial to connect rsync over non-standard port.
5. Rsync to ignore existing files
Use Rsync command with –ignore-existing option to ignore all files, which is already exists on destination.
rsync --ignore-existing -avh src/ dest/
6. Update file only if source is newer
Use --update
option to update file on remote only if there is a newer version is local. The Rsync will create file on destination if not exists. Also update file on destination of local timestamp is newer than remote system file.
rsync --update -avh src/ [email protected]:/dest/
7. Remove file from source with Rsync
Use option --remove-source-files
to remote file from source after successful file transfer to destination.
rsync --remove-source-files -avh /src [email protected]:/dest
8. Exclude specific files with Rsync
You can specify a file or pattern with --exclude=PATTERN
option to exclude from rsync.
rsync -avh --exclude '*.zip' --exclude '*.log' /src [email protected]:/dest
9. Include specific files with Rsync
The default Rsynch include all files under the source directory tree. Now, you need to include only specific files to synchronize to destination.
Here you can specify --include=PATTERN
to include specific files and exclude all files with *
.
rsync -avh -include '*.jpg' --include '*.txt' --exclude '*' /src [email protected]:/dest
10. Display Progress with Rsync
Use --progress
option to display progress during the file transfer between source and destination with Rsync command.
rsync -avh --progress /src [email protected]:/dest
11. Rsync with changing file permission
You can instruct the rsync to change files owner and group owners on destination. Use option --chown=USER:GROUP
with rsync to change file permission.
rsync -avh --chown=USER:GROUP /src [email protected]:/dest
Similarly, you can use –chmod=CHMOD option to change file or directory permissions on destination.
rsync -avh --chmod=755 /src [email protected]:/dest
12. Rsync dry-run only
Use --dry-run
option to execute dry run only. It will show you the similar results as original command but nothing will update on 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
Conclusion
In this tutorial, you have learned about rsync command in Linux with useful examples.
This will not synchronize the files that were deleted… it is a bit more complicated than this.