Use rsync on Linux to copy or synchronize files locally or over SSH. Start with --dry-run to preview the changes, confirm the destination, then run the transfer. The trailing slash on the source determines whether rsync copies the directory itself or its contents.
Install rsync on Linux
Run rsync --version to check whether it is installed. For an SSH transfer, rsync must also be available on the remote server.
- Ubuntu or Debian:
sudo apt update, thensudo apt install rsync - Fedora or current RHEL-compatible releases:
sudo dnf install rsync - Arch Linux:
sudo pacman -S rsync
Copy files locally with rsync
Use a destination directory prepared for the copy. If it is on an external disk or network mount, confirm that the intended filesystem is mounted first.
rsync -avh --dry-run --itemize-changes -- /srv/data/ /mnt/backup/data/
rsync -avh --itemize-changes -- /srv/data/ /mnt/backup/data/
The first command previews the copy. Run the second after reviewing the output. -a enables archive mode, which preserves attributes such as timestamps, permissions and symbolic links where supported. -v shows more detail, -h makes sizes readable, and --itemize-changes shows what changes for each listed item.
Why the slash matters: /srv/data/ copies the contents of data into the destination. With an existing destination directory, /srv/data copies the data directory itself underneath it.
Copy files to a remote server over SSH
Use an authorized account that can write to the destination. Replace the example hostname, login and paths with your own:
rsync -avh --dry-run --itemize-changes -e ssh -- /srv/data/ backup@example.com:/backups/data/
rsync -avh --itemize-changes -e ssh -- /srv/data/ backup@example.com:/backups/data/
If your server uses a different SSH port, put it inside the remote-shell option, such as -e 'ssh -p 2222'. Add -z only if compression helps on your link. Already compressed files may see little benefit.
Exclude files, limit speed or mirror a directory
Keep generated cache files out of a copy with an exclude pattern. Limit transfer throughput on a shared link with --bwlimit:
rsync -avh --dry-run --exclude='cache/' -- /srv/data/ /mnt/backup/data/
rsync -avh --dry-run --bwlimit=5M -- /srv/data/ /mnt/backup/data/
--bwlimit=5M limits the transfer to about 5 MiB/s when the dry-run option is removed.
Only use deletion for an intentional mirror. --delete can remove destination files missing from the source. Preview the deletions and check both paths before removing --dry-run:
rsync -avh --dry-run --itemize-changes --delete -- /srv/data/ /mnt/mirror/data/
Use rsync as part of a backup or migration
- Keep versions you can restore. A mirror can reflect overwrites or deletions from the source. Keep independent, versioned copies and test a restore before you rely on a job.
- Use a consistent source for live applications. For a running database, use a database-aware export or a supported, consistent snapshot before copying it.
- Verify a copy before retiring the source. Check the exit status, review the files at the destination, and open or restore a representative sample before deleting the original.
- Check scheduled jobs. Record transfer errors and verify that the expected mount or remote destination is available on each run.
Example: preview a space-saving snapshot
--link-dest can hard-link unchanged files to a previous snapshot. Point it at the previous copy and write to a new directory on the same backup filesystem. Keep completed snapshots intact so hard-linked files are not modified in place:
rsync -a --dry-run --itemize-changes --link-dest=/backups/day-01/ -- /srv/data/ /backups/day-02/
Troubleshoot common rsync errors
- Permission denied: Check that the chosen user can read the source and write to the destination. For remote transfers, check permissions on the remote host separately.
- SSH connection failed: Test SSH using the same hostname, user and port. Confirm rsync is installed on the remote host.
- The destination has an extra directory level: Check the trailing slash on the source path and preview the corrected command.
See the official rsync manual for option details and examples.