Copied to clipboard

rsync

rsync is a command-line utility that is used to synchronize files and directories between local and remote computers. It allows you to update files between two locations, copying only the changed parts and deleting any files that have been deleted. It can also securely transfer files over SSH.

sync files and directories locally

sync files and directories remotely

e.g. ssh -p 22

Example

1. Syncing files and directories locally

Sync source_directory/ directory to destination_directory/:


rsync -avh source_directory/ destination_directory/

Here, -a, --archive is a shortcut option, equivalent to -rlptgoD. It means to sync files and directories recursively and preserve file metadata (permissions, timestamps, etc.).

  • v, --verbose means to output verbose information.
  • h, --human-readable means to display file sizes in a human-readable way.

2. Syncing files and directories remotely

Sync local source_directory/ directory to destination_directory/ on a remote host:


rsync -avh source_directory/ user@remote_host:/path/to/destination_directory/

Here, user is the username of the remote host.

3. Secure file transfer via SSH

Sync local source_directory/ directory to destination_directory/ on a remote host via SSH:


rsync -avh -e 'ssh -p port_number' source_directory/ user@remote_host:/path/to/destination_directory/

Here, port_number is the port number of the SSH server.

4. Delete files not present at the destination

Delete files not present at the destination during the sync:


rsync -avh --delete source_directory/ destination_directory/

5. Exclude specific files and directories

Exclude specific files and directories during the sync:


rsync -avh --exclude 'file.txt' --exclude 'directory/' source_directory/ destination_directory/

6. Compress data during transfer

Compress data during transfer to reduce bandwidth usage:


rsync -avh -z source_directory/ destination_directory/

Here, -z, --compress enables compression during transfer.

7. Limit bandwidth usage

Limit bandwidth usage to 100 KB/s during transfer:


rsync -avh --bwlimit=100 source_directory/ destination_directory/

8. Test run

Perform a test run without actual transfer:


rsync -avh --dry-run source_directory/ destination_directory/

9. Display progress during transfer

Display progress during transfer:


rsync -avh --progress source_directory/ destination_directory/