20 Useful Rsync Command Examples

rsync command examples

In this article, we covered 20 useful rsync command examples that can help you transfer and synchronize files between multiple computers and devices quickly and efficiently.

Table of Contents

Introduction

Rsync is a powerful file synchronization and transfer tool that is widely used in the Linux and Unix-based operating systems. It was first released in 1996 by Andrew Tridgell and Paul Mackerras, and since then, it has been continually developed to provide a fast, efficient, and flexible method for copying and synchronizing files between different computers and devices.

Rsync Command Examples

In this article, we will cover 20 useful rsync commands that will help you to manage and transfer your files efficiently.

rsync command examples

Photo by admingeek from Infotechys

Example #1: remote server

Copy files from a local directory to a remote server:

				
					rsync -avz /path/to/local/dir username@remote_host:/path/to/remote/dir
				
			

The -avz options control the behavior of the rsync command.

  • The -a option stands for “archive mode” and preserves the permissions, ownership, timestamp, and other attributes of the files being transferred.
  • The -v option stands for “verbose” and provides detailed output about the transfer process.
  • The -z option stands for “compress” and compresses the data during transmission to save bandwidth.

Example #2: local directory

Copy files from a remote server to a local directory:

				
					rsync -avz username@remote_host:/path/to/remote/dir /path/to/local/dir
				
			

This command copies the contents of the remote directory at /path/to/remote/dir to the local directory at /path/to/local/dir.

Example #3: specific Ports

Copy files using a specific SSH port:

				
					rsync -avz -e "ssh -p 2222" /path/to/local/dir username@remote_host:/path/to/remote/dir
				
			

We reviewed the -avz options in the first example.

  • -essh -p 2222“: option that specifies the remote shell to use for the transfer. In this case, it’s the SSH protocol on port 2222.
  • /path/to/local/dir: the path of the local directory that is being transferred.
  • username@remote_host:/path/to/remote/dir: the remote directory where the files will be transferred. The “username” is the username on the remote host, and “remote_host” is the hostname or IP address of the remote host.

Example #4: exclude option

Exclude specific files or directories from the transfer:

				
					rsync -avz --exclude='*.log' /path/to/local/dir username@remote_host:/path/to/remote/dir
				
			

This command uses rsync to transfer files from a local directory to a remote directory while excluding any files with the .log extension.

Example #5: archive mode

Copy files in archive mode, preserving permissions, ownership, and timestamps:

				
					rsync -avz --archive /path/to/local/dir username@remote_host:/path/to/remote/dir
				
			

This command copies the contents of /path/to/local/dir to the remote server at remote_host, while preserving permissions, ownership, and timestamps of the original files.

Example #6: change updates

Copy only the files that have changed since the last transfer:

				
					rsync -avz --update /path/to/local/dir username@remote_host:/path/to/remote/dir
				
			

This command copies only the files that have been modified or added since the last transfer, from /path/to/local/dir to /path/to/remote/dir.

Example #7: delete updates

Copy files and delete any files on the remote server that are not in the local directory:

				
					rsync -avz --delete /path/to/local/dir username@remote_host:/path/to/remote/dir
				
			

This command copies the contents of /path/to/local/dir to the remote server at remote_host and deletes any files on the remote server that are not in the local directory.

Example #8: limit bandwidth

Limit the bandwidth used for the transfer:

				
					rsync -avz --bwlimit=1000 /path/to/local/dir username@remote_host:/path/to/remote/dir
				
			

This command limits the bandwidth of the transfer to 1000 kilobits per second, while copying the contents of /path/to/local/dir to the remote server at remote_host.

Example #9: show progress

Show the progress of the transfer:

				
					rsync -avz --progress /path/to/local/dir username@remote_host:/path/to/remote/dir
				
			

This command shows the progress of the transfer while copying the contents of /path/to/local/dir to the remote server at remote_host (also referred to as -P).

Example #10: compress files

Copy files and compress them during the transfer:

				
					rsync -avz --compress /path/to/local/dir username@remote_host:/path/to/remote/dir
				
			

This command compresses the files during the transfer, while copying the contents of /path/to/local/dir to the remote server at remote_host.

Example #11: delete dirs

Copy files and delete any empty directories on the remote server:

				
					rsync -avz --delete-empty-dirs /path/to/local/dir username@remote_host:/path/to/remote/dir
				
			

This command deletes empty directories on the remote server during the transfer from the local directory to a remote server.

Example #12: hidden files

Copy files and exclude hidden files and directories:

				
					rsync -avz --exclude='.*' /path/to/local/dir username@remote_host:/path/to/remote/dir
				
			

This command copies files from the local directory to a remote server, while excluding all hidden files and directories.

Example #13: hard links

Copy files and preserve hard links:

				
					rsync -avz --hard-links /path/to/local/dir username@remote_host:/path/to/remote/dir
				
			

This command preserves hard links during the transfer from the local directory to a remote server.

Example #14: SELinux contexts

Copy files and preserve SELinux contexts:

				
					rsync -avz -X -A -S /path/to/local/dir username@remote_host:/path/to/remote/dir
				
			

This command preserves SELinux contexts during the transfer from the local directory to a remote server.

Example #15: ACLs

Copy files and preserve ACLs:

				
					rsync -avz -A /path/to/local/dir username@remote_host:/path/to/remote/dir
				
			

This command preserves ACLs during the transfer from the local directory to a remote server.

Example #16: Checksum

Copy files and use a different file checksum algorithm:

				
					rsync -avz --checksum=sha256 /path/to/local/dir username@remote_host:/path/to/remote/dir
				
			

This command uses the SHA256 checksum algorithm to verify file integrity during the transfer from the local directory to a remote server.

Example #17: Attributes

Copy files and preserve extended attributes:

				
					rsync -avz -X /path/to/local/dir username@remote_host:/path/to/remote/dir
				
			

This command preserves extended attributes during the transfer from the local directory to a remote server.

Example #18: exclude-from

Copy files and exclude files based on their modification time:

				
					rsync -avz --exclude-from=/path/to/exclude-file /path/to/local/dir username@remote_host:/path/to/remote/dir
				
			

This command excludes files from the transfer based on their modification time using the exclude-file list.

Example #19: ignore existing

Don’t update files that already exist:

				
					rsync -avz --ignore-existing /path/to/local/dir username@remote_host:/path/to/remote/dir
				
			

The --ignore-existing option that tells rsync to skip transferring files that already exist on the remote host. This can be useful when you only want to transfer files that are new or have been updated.

Example #20: Symbolic links

Preserve ownership, permissions, timestamps, symlinks, while displaying progress and verbose output during transfer:

				
					rsync -Pavl /path/to/local/dir username@remote_host:/path/to/remote/dir
				
			

 The command “rsync -Pavl /path/to/local/dir username@remote_host:/path/to/remote/dir” will synchronize the files in the local directory “/path/to/local/dir” with the remote directory “/path/to/remote/dir” on the host “remote_host“, preserving permissions, ownership, timestamps, and symbolic links, and displaying progress and verbose output during the transfer.

Each of these rsync commands offers a specific functionality that can be helpful in different situations. Knowing the right command to use can save time and effort in transferring files between local and remote servers.

Conclusion

Rsync is an incredibly versatile and powerful tool that can be used for a wide variety of file transfer and synchronization tasks. By learning these 20 useful rsync commands, you can take advantage of all of the features and functionality that this tool has to offer. Whether you need to transfer files between local and remote servers, synchronize files across multiple devices, or backup your important data, rsync is the perfect tool for the job. With these commands in your arsenal, you’ll be able to work with rsync more efficiently and effectively than ever before.

Related Posts

Leave a Reply

Your email address will not be published. Required fields are marked *