Rsync
Using the rsync
command in Bash
, you can synchronize the contents of a directory between two locations, either locally or across different machines over a network. Here's the basic syntax and some examples:
Synta
rsync [options] source_directory destination
Examples
Local Sync:
To synchronize a directory named "source" to a directory named "destination" locally:
rsync -av source/ destination/
-a
: Archive mode, preserving file permissions, ownership, timestamps, etc.-v
: Verbose mode, showing details of the synchronization process.
Remote Sync:
To synchronize a local directory to a remote machine over SSH:
rsync -av source/ user@remote_host:destination/
- Replace
user
with the remote machine's username. - Replace
remote_host
with the IP address or hostname of the remote machine. - Ensure you have SSH access to the remote machine and the necessary permissions.
- Replace
Excluding Files:
You can exclude specific files or patterns using the
--exclude
option. For example, excluding all.txt
files:rsync -av --exclude '*.txt' source/ destination/
Progress Information:
To see progress information during synchronization, you can use the
--progress
option:rsync -av --progress source/ destination/
Dry Run (Preview):
To see what changes would be made without actually executing the synchronization, use the
--dry-run
option:rsync -av --dry-run source/ destination/
Deleting Extraneous Files:
By default,
rsync
does not delete files in the destination that are not in the source. To delete extraneous files in the destination, use the--delete
option:rsync -av --delete source/ destination/
These examples cover some of the common use cases for rsync
. The command offers many more options and functionalities for more complex synchronization scenarios.
Always refer to the rsync
documentation (man rsync
) for a complete list of options and their explanations.
✅ Resources
- 👉 Bash CheatSheet
- 👉 Deploy Projects using your own cloud provider
- 👉 Get Deployment Support from
experts