How to use the command 'rsync' (with examples)
Rsync is a powerful utility for efficiently transferring and synchronizing files across computer systems. Its primary function is to enable file transfer, either locally or over a network. It is commonly used for backups and mirroring because of its ability to minimize data transfer by only sending differences between the source files and the existing files in the destination. Rsync typically operates over SSH for secure data transfer and provides a variety of options that enhance its flexibility and control.
Use case 1: Transfer a file
Code:
rsync path/to/source path/to/destination
Motivation:
This use case is ideal when you need to quickly transfer a single file from one location to another. It’s straightforward and minimizes computation by not engaging any additional features or options. It’s perfect for one-time transfers or when dealing with simple file operations.
Explanation:
The command specifies two paths: the source and the destination. Rsync transfers the file from the specified source path to the destination path. No extra options are used in this basic example, making it a simple file copying task.
Example Output:
sent 234 bytes received 35 bytes 538.00 bytes/sec
total size is 128 speedup is 0.47
Use case 2: Use archive mode (recursively copy directories, copy symlinks without resolving, and preserve permissions, ownership and modification times)
Code:
rsync -a path/to/source path/to/destination
Motivation:
Archive mode is essential when you want to retain the file system properties like directory structures, symlinks, and file permissions during transfers. It’s particularly useful for creating backups or moving project directories where these attributes are crucial.
Explanation:
The -a
or --archive
option is a compound option that combines several others: -r
(recursive), -l
(copy symlinks as symlinks), -p
(preserve permissions), -t
(preserve modification times), -g
(preserve group), and -o
(preserve owner). It is designed to mirror a directory tree precisely and is often employed for comprehensive backups or migrations.
Example Output:
sending incremental file list
.[...list of files...]
sent 248 bytes received 48 bytes 192.00 bytes/sec
total size is 608 speedup is 2.14
Use case 3: Compress the data as it is sent to the destination, display verbose and human-readable progress, and keep partially transferred files if interrupted
Code:
rsync -zvhP path/to/source path/to/destination
Motivation:
This usage is fitting when transferring large files or over slower network connections as it reduces the amount of data sent. Including progress visibility helps monitor the transfer, and preserving partially transferred files ensures resumability without needing to start over if the process is interrupted.
Explanation:
-z
or--compress
: Compresses file data as it is sent to reduce bandwidth usage.-v
or--verbose
: Increases verbosity, providing detailed output about the transfer process.-h
or--human-readable
: Makes sizes in output more understandable (e.g., in KB, MB).-P
: Combines both--partial
(keeps partially transferred files) and--progress
(shows progress during transfer).
Example Output:
sending incremental file list
file1.txt
1.62M 100% 2.35MB/s 0:00:00 (xfer#1, to-check=0/1)
sent 1.68M bytes received 35 bytes 302.09K bytes/sec
total size is 1.62M speedup is 0.96
Use case 4: Recursively copy directories
Code:
rsync -r path/to/source path/to/destination
Motivation:
This command is particularly helpful when you need to copy all files, including subdirectories within a directory. It ensures all contents are transferred without needing manual intervention for individual files.
Explanation:
The -r
or --recursive
option tells rsync to enter directories and copy data recursively, thus covering all nested subdirectories and files.
Example Output:
sending incremental file list
dir/
dir/file1
dir/file2
sent 248 bytes received 56 bytes 203.00 bytes/sec
total size is 2.5K speedup is 8.00
Use case 5: Transfer directory contents, but not the directory itself
Code:
rsync -r path/to/source/ path/to/destination
Motivation:
Sometimes, you might want to transfer only the contents of a directory and not the directory structure itself. This command helps in copying only the inner files and folders.
Explanation:
By appending a slash /
at the end of the source directory path, rsync recognizes it should copy the contents rather than the directory as a whole unit. This subtle change alters the transfer behavior significantly.
Example Output:
sending incremental file list
fileA
fileB
sent 248 bytes received 57 bytes 203.00 bytes/sec
total size is 2.5K speedup is 7.82
Use case 6: Use archive mode, resolve symlinks, and skip files that are newer on the destination
Code:
rsync -auL path/to/source path/to/destination
Motivation:
This variant is ideal for updating backups or file collections where you need to ensure newer files remain as they are while older files are updated. It helps sync changes efficiently without overwriting newer files at the destination.
Explanation:
-u
or--update
: Skips files that are newer at the destination.-L
or--copy-links
: Treats symlinks as regular files and copies the linked data.
Example Output:
sending incremental file list
older_file.txt
sent 106 bytes received 20 bytes 252.00 bytes/sec
total size is 230.00K speedup is 1798.41
Use case 7: Transfer a directory from a remote host running rsyncd
and delete files on the destination that do not exist on the source
Code:
rsync -r --delete rsync://host:path/to/source path/to/destination
Motivation:
This is useful for maintaining an exact mirror of a directory on a local system. Deleting files not present on the source ensures the destination is an identical replication, critical for mirrored backups and synchronization.
Explanation:
--delete
: Deletes files from the destination that don’t exist on the source, ensuring exact mirroring.- The
rsync://host:path
specifies the source on the remote host using rsync daemon for transfers.
Example Output:
receiving incremental file list
deleting obsolete_file.txt
sent 68 bytes received 1.24K bytes 412.00 bytes/sec
total size is 849 speedup is 0.65
Use case 8: Transfer a file over SSH using a different port than the default (22) and show global progress
Code:
rsync -e 'ssh -p port' --info=progress2 host:path/to/source path/to/destination
Motivation:
This configuration is necessary when accessing a remote host with SSH on a non-standard port. Providing global progress helps manage larger transfers effectively.
Explanation:
-e 'ssh -p port'
: Specifies the remote shell to use along with the designated port.--info=progress2
: Provides a global progress indicator showing the overall progress rather than per-file progress.
Example Output:
3.00M 100% 2.38MB/s 0:00:01 (xfr#1, to-check=0/1)
sent 100 bytes received 3.12M bytes 425.47K bytes/sec
Conclusion:
Rsync is a versatile command-line utility for efficient data transfer and synchronization. Whether you need to mirror directories, preserve file attributes, or transfer over networks with compression and custom ports, rsync’s extensive options cater to diverse needs. Its ability to minimize data transfer by sending only differences, alongside the possibility to encrypt transfers via SSH, makes it an indispensable tool for administrators and users requiring precise syncing capabilities.