How to use the rsync command in Linux

By Angus Published 20 February 2025 Updated 4 March 2026 4 min read

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.

Before you begin

  • You need SSH access to your server.
  • The rsync package must be installed on both source and destination systems.
  • We recommend testing commands on non-critical directories first.

Copy files locally with rsync

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.

  1. Connect to your server via SSH.
    Open your terminal and log in using your SSH credentials.
  2. Run the rsync command with the recursive flag.
    Replace [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.

Preserve file attributes during transfer

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.

  1. Add the archive flag to your rsync command.
    Replace the -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.

Monitor transfer progress

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.

  1. Add the progress flag to your command.
    Combine -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.

Sync files to a remote server over SSH

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.

  1. Specify the remote destination in your rsync command.
    Replace [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.

Optimise remote transfers with compression

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.

  1. Add compression and SSH options to your command.
    Replace [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.

Wrapping up

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.

Need more power?

Get scalable resources with our VPS hosting with root access and optional software.

Get VPS Hosting

Starting something new?

Perfect for websites and small businesses unlimited bandwidth with cPanel hosting.

Get cPanel Hosting