Come hang with us on Discord and chat directly with the team!Discordtop-bar-close-icon

2024-09-20

How to Use Rsync to Sync Local and Remote Directories

tutorials
img

Rsync is a powerful tool for synchronizing files and directories between local and remote systems. It is widely used in Linux/Unix environments for its efficiency and flexibility in data transfer tasks.

Prerequisites

To practice using rsync, you will need two machines: one to act as your local computer and another as your remote machine.

Basic Rsync Command

The basic syntax for rsync is as follows:

rsync [options] source destination

Synchronizing Directories

To synchronize a local directory with a remote directory, you can use the following command:

rsync -avz /local/directory/ remote_user@remote_host:/remote/directory/

- -a: Archive mode, which preserves permissions, times, symbolic links, and more.
- -v: Verbose output, showing the progress of the synchronization.
- -z: Compresses data during transfer for faster performance.

Using Rsync with SSH

Rsync can be used over SSH for secure data transfer. If your SSH server is running on a non-standard port, you can specify it using the -e option:

rsync -avz -e "ssh -p 2222" /local/directory/ remote_user@remote_host:/remote/directory/

Deleting Files

To ensure that the destination directory is an exact copy of the source, including deletions, use the --delete option:

rsync -avz --delete /local/directory/ remote_user@remote_host:/remote/directory/

Conclusion

Rsync is a versatile tool that can handle a variety of synchronization tasks between local and remote directories. By using options like -a, -v, -z, and --delete, you can customize the synchronization process to meet your specific needs.