How to Use the rsync Command to Sync Files and Directories on Linux
The rsync command is one of the most powerful tools in the Linux toolkit for syncing files and directories. Whether you’re syncing locally or transferring files to a remote server, rsync
combines speed, flexibility, and efficiency. Recently, I setup a process to synchronize the contents of directories in my home SAN and it worked great. In this post, we’ll explore the basics of rsync, how it works, and some of its most useful options.
What is rsync
?
rsync
(remote sync) is a file-copying tool that uses a unique algorithm to minimize data transfer by copying only the differences between source and destination files. This makes it ideal for backups, mirroring directories, or syncing files over a network.
The basic syntax is straightforward:
rsync [options] source destination
Basic Local Sync Example
To sync a local directory, use the following command:
rsync -av /source/directory/ /destination/directory/
What’s Happening Here?
-a
(archive): Preserves symbolic links, permissions, timestamps, and more.-v
(verbose): Outputs detailed information about what’s being synced.- The trailing
/
ensures only the contents of the source directory are copied, not the directory itself.
Syncing to a Remote Server
To sync files or directories to a remote server, combine rsync
with SSH:
rsync -av /local/directory/ user@remote_host:/remote/directory/
And to pull files from the server back to your local machine:
rsync -av user@remote_host:/remote/directory/ /local/directory/
By default, rsync
uses SSH for secure transfers. If you need a different protocol, you can specify it with the -e
flag.
Useful Options for rsync
rsync
offers many options to customize its behavior. Here are some of the most commonly used ones:
--delete
: Removes files from the destination that no longer exist in the source. Be cautious with this option to avoid unintended data loss.
rsync -av --delete /source/directory/ /destination/directory/
--exclude
: Excludes specific files or directories.
rsync -av --exclude '*.log' /source/ /destination/
--dry-run
: Simulates the sync process without making any changes. This is especially useful for testing your command before running it.
rsync -av --dry-run /source/ /destination/
-z
: Compresses files during transfer to save bandwidth, particularly useful for remote syncing.--progress
: Displays progress for each file during the sync.
Advanced Example: Backing Up a Website
Let’s say you want to back up a website hosted on a remote server to your local machine:
rsync -azv --delete user@remote_host:/var/www/html/ /local/backup/html/
In this command:
- -z: Compresses files during transfer.
--delete
: Deletes files in the local backup that no longer exist on the server.- -v: Provides detailed output about what’s being synced.
Why Use rsync
?
- Efficiency: Only changes are transferred, saving time and bandwidth.
- Flexibility: Works locally and remotely, with extensive options for customization.
- Reliability: Maintains file permissions, timestamps, and symbolic links, making it ideal for backups.
The rsync
command is a must-have tool for anyone managing files on Linux. Whether you’re syncing directories locally, backing up to a remote server, or automating file transfers, rsync
is fast, reliable, and highly configurable. Start with the basic options and experiment with advanced features like --exclude
and --delete
to optimize your workflow.
Do you have any tricks using rsync or other tools? Share them in the comments.
More info:
Average Rating