
{{ $('Map tags to IDs').item.json.title }}
How to Sync Files with rsync
rsync is a powerful and versatile command-line utility in Linux used for synchronizing files and directories between two locations swiftly and efficiently. It can be used for local file synchronization, as well as for remote file transfers over SSH. This tutorial will guide you through the basics of using rsync for file synchronization.
1. Installing rsync
rsync is commonly pre-installed on most Linux distributions. To check if it’s installed, run:
rsync --version
If not installed, you can install it using:
- For Ubuntu:
sudo apt install rsync
- For CentOS:
sudo yum install rsync
2. Basic rsync Command Syntax
The basic syntax of the rsync command is as follows:
rsync [options] source destination
Where source
is the file or directory you want to sync, and destination
is the target location.
3. Syncing Local Files
To synchronize a local file to another location, use:
rsync -avh /path/to/source/file.txt /path/to/destination/
The options used:
- -a: Archive mode, which preserves permissions, timestamps, and symlinks.
- -v: Verbose output, providing detailed progress information.
- -h: Human-readable output for file sizes.
4. Syncing Directories
To sync an entire directory (including its contents), use a trailing slash to include the content:
rsync -avh /path/to/source/directory/ /path/to/destination/directory/
This synchronizes all the files and subdirectories from the source to the destination directory.
5. Syncing Files Over SSH
You can also sync files to a remote server using SSH:
rsync -avh -e ssh /path/to/local/file.txt user@remote_host:/path/to/remote/destination/
Replace user
and remote_host
with the actual username and host address of the remote server.
6. Deleting Extraneous Files
If you want to ensure that the destination directory matches the source exactly and remove any files that are no longer present in the source, use the --delete
option:
rsync -avh --delete /path/to/source/ /path/to/destination/
7. Conclusion
By following this tutorial, you have learned how to use the rsync
command to sync files and directories effectively in Linux. rsync is an invaluable tool for backups and file transfer, offering flexibility and efficiency. Continue to explore its various options and features to enhance your file management capabilities!