40 Practical Rsync Command Examples

40 Practical Rsync Command Examples

Rsync, short for “remote sync,” is a versatile tool for data synchronization and backup in Unix-based systems. It is renowned for its speed and efficiency, allowing for incremental file transfers. Rsync only copies the differences between source and destination, which makes it ideal for backup and mirroring tasks. This article will explore 40 practical rsync command examples to help you master its usage.

Practical Examples of Rsync Command in Combination with SSH

1. Basic Rsync Over SSH

To transfer files from a local machine to a remote server using SSH, use the following command:

rsync -avz -e ssh /local_directory/ user@remote_host:/remote_directory/ 
  • -a: Archive mode, preserving permissions, timestamps, and symlinks.
  • -v: Verbose mode for detailed output.
  • -z: Compresses data during transfer to save bandwidth.
  • -e ssh: Specifies SSH as the remote shell.

This command securely transfers all files from /local_directory/ to /remote_directory/ on the remote server.

2. Rsync with SSH Key Authentication

For passwordless authentication, use an SSH key. Here’s how you can use it:

rsync -avz -e 'ssh -i ~/.ssh/id_rsa' /local_directory/ user@remote_host:/remote_directory/ 
  • -i ~/.ssh/id_rsa: Specifies the SSH private key for authentication.

This command uses the private key id_rsa for authentication, bypassing the need for a password.

3. Syncing Files from Remote to Local

You can also use Rsync to sync files from a remote server to your local machine:

rsync -avz -e ssh user@remote_host:/remote_directory/ /local_directory/ 

This command downloads all files from /remote_directory/ on the server to /local_directory/ on your machine.

4. Using a Non-Standard SSH Port

If your SSH server listens on a non-standard port, specify the port number:

rsync -avz -e 'ssh -p 2222' /local_directory/ user@remote_host:/remote_directory/ 
  • -p 2222: Tells SSH to connect to port 2222 instead of the default port 22.

This command is useful when the SSH service runs on a different port for security reasons.

5. Limiting Bandwidth Usage During Transfer

To prevent Rsync from using too much bandwidth, you can limit it:

rsync -avz --bwlimit=500 -e ssh /local_directory/ user@remote_host:/remote_directory/ 
  • --bwlimit=500: Limits the transfer speed to 500 KB/s.

This is useful when you need to keep network bandwidth available for other tasks.

6. Excluding Files and Directories

You might not want to transfer certain files or directories. Here’s how to exclude them:

rsync -avz --exclude 'file.txt' -e ssh /local_directory/ user@remote_host:/remote_directory/ 
  • --exclude 'file.txt': Excludes file.txt from being transferred.

You can also exclude entire directories by specifying their names.

7. Running Rsync in Background

To run Rsync in the background, use the nohup command along with an ampersand:

nohup rsync -avz -e ssh /local_directory/ user@remote_host:/remote_directory/ & 
  • nohup: Prevents the process from being terminated when you log out.
  • &: Runs the process in the background.

This is useful for large transfers that take a long time to complete.

8. Synchronizing Only New or Updated Files

To sync only new or updated files, use the -u option:

rsync -avzu -e ssh /local_directory/ user@remote_host:/remote_directory/ 
  • -u: Skips files that are newer on the destination.

This command ensures that only newer files are copied, avoiding unnecessary data transfer.

9. Performing a Dry Run Before Actual Sync

To see what Rsync would do without making any changes, perform a dry run:

rsync -avz --dry-run -e ssh /local_directory/ user@remote_host:/remote_directory/ 
  • --dry-run: Simulates the transfer without actually copying any files.

This is a safe way to preview changes before committing to them.

10. Logging Rsync Output to a File

To keep a record of the Rsync operation, log the output to a file:

rsync -avz -e ssh --log-file=rsync.log /local_directory/ user@remote_host:/remote_directory/ 
  • --log-file=rsync.log: Writes the output to rsync.log.

This is useful for troubleshooting or keeping a history of file transfers.

1. Basic Rsync Command

The most straightforward rsync command copies a file from a source to a destination.

rsync source_file.txt /destination_directory/ 

This command copies source_file.txt to the specified destination directory.

2. Copy a Directory

To copy an entire directory, use the -r option, which stands for “recursive.”

rsync -r /source_directory/ /destination_directory/ 

This copies all contents of /source_directory/ to /destination_directory/.

3. Preserve File Permissions

To preserve file permissions, use the -a option, which stands for “archive.”

rsync -a /source_directory/ /destination_directory/ 

The -a option also preserves symbolic links, device files, and ownership.

4. Synchronize Directories

Synchronizing directories ensures that both the source and destination contain identical files.

rsync -av /source_directory/ /destination_directory/ 

The -v option adds verbosity, showing progress and file details during the transfer.

5. Delete Files at the Destination

To delete files at the destination that no longer exist at the source, use the --delete option.

rsync -av --delete /source_directory/ /destination_directory/ 

This keeps the destination directory in perfect sync with the source.

6. Exclude Specific Files or Directories

To exclude certain files or directories from being copied, use the --exclude option.

rsync -av --exclude 'file.txt' /source_directory/ /destination_directory/ 

This excludes file.txt from being copied.

7. Exclude Multiple Files or Directories

You can exclude multiple files or directories by repeating the --exclude option.

rsync -av --exclude 'file1.txt' --exclude 'dir1/' /source_directory/ /destination_directory/ 

This command excludes both file1.txt and the directory dir1/.

8. Include Only Specific Files or Directories

To include only specific files or directories, use the --include option followed by --exclude '*'.

rsync -av --include 'file.txt' --exclude '*' /source_directory/ /destination_directory/ 

This includes only file.txt in the transfer.

9. Show Progress During Transfer

To display progress during the file transfer, use the --progress option.

rsync -av --progress /source_directory/ /destination_directory/ 

This shows how much of each file has been transferred.

10. Limit Bandwidth Usage

To limit the bandwidth used by rsync, use the --bwlimit option followed by the desired rate in kilobytes per second.

rsync -av --bwlimit=1000 /source_directory/ /destination_directory/ 

This limits the transfer speed to 1000 KB/s.

11. Compress Files During Transfer

To compress files during transfer, use the -z option.

rsync -avz /source_directory/ /destination_directory/ 

This reduces the amount of data sent over the network.

12. Sync Files Over SSH

To sync files over SSH, use the -e option followed by ssh.

rsync -avz -e ssh /source_directory/ user@remote_host:/destination_directory/ 

This transfers files securely over SSH.

13. Use a Non-Standard SSH Port

If SSH runs on a non-standard port, specify the port number with the -p option inside -e.

rsync -avz -e 'ssh -p 2222' /source_directory/ user@remote_host:/destination_directory/ 

This uses port 2222 for the SSH connection.

14. Sync Only Updated Files

To sync only files that have been updated, use the -u option.

rsync -avzu /source_directory/ /destination_directory/ 

This avoids overwriting newer files at the destination.

15. Perform Dry Run Before Actual Sync

To simulate the rsync command without making changes, use the --dry-run option.

rsync -av --dry-run /source_directory/ /destination_directory/ 

This shows what would happen without actually performing the sync.

16. Copy Files from Remote to Local

To copy files from a remote server to your local machine, reverse the source and destination paths.

rsync -avz user@remote_host:/remote_directory/ /local_directory/ 

This downloads files from the remote server.

17. Copy Files from Local to Remote

To copy files from your local machine to a remote server, specify the remote destination.

rsync -avz /local_directory/ user@remote_host:/remote_directory/ 

This uploads files to the remote server.

18. Preserve Hard Links

To preserve hard links, use the -H option.

rsync -avH /source_directory/ /destination_directory/ 

This ensures that hard links remain intact during the transfer.

19. Preserve Modification Times

To preserve file modification times, use the -t option.

rsync -avt /source_directory/ /destination_directory/ 

This keeps the original modification times of the files.

20. Preserve Ownership

To preserve file ownership, use the -o option.

rsync -av --owner /source_directory/ /destination_directory/ 

This maintains the original ownership of the files.

21. Preserve Group Ownership

To preserve group ownership, use the -g option.

rsync -av --group /source_directory/ /destination_directory/ 

This retains the original group ownership.

22. Archive and Sync in One Command

The -a option combines multiple options: -r, -l, -p, -t, -g, -o, and -D.

rsync -a /source_directory/ /destination_directory/ 

This is a comprehensive option for archiving and syncing files.

23. Ignore Existing Files at Destination

To ignore files that already exist at the destination, use the --ignore-existing option.

rsync -av --ignore-existing /source_directory/ /destination_directory/ 

This skips files that are already present at the destination.

24. Copy Directory Structure Only

To copy only the directory structure without files, use the --include and --exclude options together.

rsync -av --include '*/' --exclude '*' /source_directory/ /destination_directory/ 

This creates an empty directory structure at the destination.

25. Synchronize Files Based on Modification Time

To synchronize files based on modification time, use the -u option.

rsync -avzu /source_directory/ /destination_directory/ 

This ensures only newer files are copied.

26. Log Rsync Output to a File

To log the rsync output to a file, use the --log-file option.

rsync -av --log-file=rsync.log /source_directory/ /destination_directory/ 

This saves the output to rsync.log.

27. Skip Files Larger Than a Specified Size

To skip files larger than a specified size, use the --max-size option.

rsync -av --max-size='100M' /source_directory/ /destination_directory/ 

This skips files larger than 100 MB.

28. Skip Files Smaller Than a Specified Size

To skip files smaller than a specified size, use the --min-size option.

rsync -av --min-size='10M' /source_directory/ /destination_directory/ 

This skips files smaller than 10 MB.

29. Use Checksum for File Comparison

To use checksums for file comparison instead of file size and modification time, use the -c option.

rsync -avc /source_directory/ /destination_directory/ 

This compares files based on their checksums.

30. Rsync as a Daemon

To run rsync as a daemon, use the --daemon option.

rsync --daemon 

This starts an rsync server for listening to incoming requests.

Conclusion

Rsync is a powerful tool that can handle a wide range of file transfer and synchronization tasks. Whether you are backing up your data, mirroring directories, or syncing files across remote servers, rsync offers efficient and flexible options. By mastering these 40 practical rsync commands, you can take full advantage of its capabilities and simplify your file management tasks.

Fedya Serafiev

Fedya Serafiev

Fedya Serafiev owns the website linuxcodelab.eu. He finds satisfaction in helping people solve even the most complex technical problems. His current goal is to write easy-to-follow articles so that such problems do not arise at all.

Thank you for reading the article! If you found the information useful, you can donate using the buttons below: