When you need to copy massive amounts of data, back up a server, or migrate to a new system in Linux, the standard cp command is not enough. The rsync (Remote Sync) utility is the professional’s choice. It is incredibly fast, can resume interrupted transfers, and features a “delta-transfer algorithm” that only copies the differences between files, saving enormous amounts of bandwidth and time.
The Basic Syntax and Essential Flags
The basic structure of an rsync command is:
rsync [options] source destination
While there are dozens of options, 99% of the time you will use this specific combination of flags: -avz
-a(Archive): The most important flag. It copies folders recursively and preserves all permissions, timestamps, and symbolic links.-v(Verbose): Prints what the command is doing to the screen so you can monitor the progress.-z(Compress): Compresses file data during the transfer, significantly speeding up network copies.
Common Rsync Examples
1. Copying to a Remote Server (Push)
To back up a local folder to a remote server over SSH:
rsync -avz /local/folder/ user@remote-ip:/remote/destination/
2. Copying from a Remote Server (Pull)
To download files from a remote server to your local machine:
rsync -avz user@remote-ip:/remote/folder/ /local/destination/
3. Mirroring with the Delete Flag
By default, rsync only adds or updates files. If you delete a file in the source, it will remain in the destination. To make the destination an exact mirror of the source, add the --delete flag. Warning: Be very careful with this flag, as it will permanently delete files in the destination folder that do not exist in the source.
rsync -avz --delete /source/dir/ /destination/dir/
Crucial Rule: The Trailing Slash
When using rsync, the trailing slash / at the end of the source directory name changes how the command behaves completely.
- With a trailing slash (
/source/dir/): This copies the contents of the directory into the destination. - Without a trailing slash (
/source/dir): This copies the directory itself (creating a folder named “dir” inside the destination) and all its contents.
To avoid mistakes, it is highly recommended to add the --dry-run (or -n) flag when trying a complex command for the first time. This simulates the transfer and shows you exactly what files would be copied or deleted without actually doing it.