The rsync command synchronises files and directories between locations while transferring only what has changed. This makes it faster and more efficient than basic copying tools when you need to back up directories, mirror file structures or transfer data between servers.
You will learn how to use rsync to copy files locally, preserve file attributes during transfers and sync directories remotely over SSH. These techniques reduce bandwidth usage and save time on repeated backup operations.
You will copy a directory and its contents from one location to another on the same server. The -r flag tells rsync to process directories recursively, transferring all files and subdirectories inside the source path.
[source-path] with the directory you want to copy and [destination-path] with where you want to place it. The trailing slash on the source path copies the directory’s contents rather than the directory itself.rsync -r /home/cpusername/public_html/ /home/cpusername/backup/
This command transfers only new or modified files on subsequent runs, which reduces transfer time compared to copying everything again.
The -a flag enables archive mode, which preserves permissions, timestamps, symbolic links and ownership during the transfer. This ensures the destination matches the source exactly, which is necessary for accurate backups and migrations.
-r flag with -a to enable attribute preservation.rsync -a /home/cpusername/public_html/ /home/cpusername/backup/
Archive mode combines several options into one flag, making it the standard choice for backup operations where file metadata matters.
The -P flag displays transfer progress and preserves partially transferred files if the connection drops. This helps you track large transfers and resume them without starting over.
-a and -P to show detailed transfer statistics while preserving file attributes.rsync -aP /home/cpusername/public_html/ /home/cpusername/backup/
You will see the completion percentage, transfer speed and estimated time remaining for each file as it copies.
You can transfer files between servers by specifying a remote destination in the format username@hostname:path. Rsync uses SSH for the connection, which encrypts the transfer and authenticates using your SSH credentials.
[ssh-username] with your remote server username, [remote-address] with the server’s IP address or hostname, and [destination-path] with the target directory on the remote server.rsync -a /home/cpusername/public_html/ sshusername@remoteaddress:/home/cpusername/backup/
This command preserves file attributes during the remote transfer. Rsync only sends changed data, which reduces bandwidth usage compared to copying entire directories repeatedly.
Adding the -z flag compresses data during transfer, which speeds up the process and reduces bandwidth consumption. The -e option lets you specify SSH connection parameters, including custom ports.
[port-number] with your SSH port if it differs from the default port 22. Replace [remote-ip] with your server’s IP address.rsync -azP -e "ssh -p 9248" /home/cpusername/public_html/ root@192.168.1.1:/home/cpusername/backup/
This command combines archive mode, compression, progress display and a custom SSH port. The -P flag also preserves partially transferred files if the connection drops, allowing you to resume the transfer without starting over.
You now know how to use rsync to copy files locally, preserve file attributes and sync directories remotely over SSH. These techniques reduce transfer times and bandwidth usage by sending only changed data between locations.
Test your rsync commands on non-critical directories before running them on production data. Review our guides on using the SCP command and copying files with the cp command for alternative file transfer methods. Our VPS hosting includes full SSH access for running rsync and other command-line tools.
Get scalable resources with our VPS hosting with root access and optional software.
Get VPS HostingPerfect for websites and small businesses unlimited bandwidth with cPanel hosting.
Get cPanel Hosting