How To

How to Transfer Files with rsync over SSH: A Practical Guide

rsync over SSH combines secure encrypted transport with fast incremental transfers. Instead of copying every file on every run, rsync compares source and destination and sends only the differences — making repeat transfers of large directories much faster than scp.

Before starting, install rsync on both the source and destination machines. Installing it only on the local system is the most common setup mistake:

bashsudo apt install rsync      # Ubuntu, Debiansudo dnf install rsync      # Fedora, RHEL

How to Transfer Files with rsync over SSH

The general syntax:

bash# Local to remotersync [OPTIONS] /local/source user@host:/remote/dest/# Remote to localrsync [OPTIONS] user@host:/remote/source /local/dest/

Newer versions of rsync use SSH as the default remote shell, so -e ssh can be omitted in most cases.

The -a flag (archive mode) is the standard choice for transfers. It is equivalent to -rlptgoD and preserves symlinks, permissions, timestamps, group, owner, and device files recursively:

bashrsync -a /opt/file.zip user@12.12.12.12:/var/www/

On success, no output is produced. If the file already exists on the remote server it is overwritten. To save it under a different name, specify the filename in the destination path:

bashrsync -a /opt/file.zip user@12.12.12.12:/var/www/file2.zip

Reverse the order to pull a file from a remote server to local:

bashrsync -a user@12.12.12.12:/var/www/file.zip /opt/

Trailing Slash, Dry Run, and the –delete Option

The trailing slash rule is the most important rsync behavior to understand before transferring directories:

bashrsync -a /src/ user@host:/dest/    # copies CONTENTS of src into destrsync -a /src  user@host:/dest/    # copies the src DIRECTORY ITSELF into dest

Without the trailing slash, rsync creates a src/ subdirectory inside dest/. This is the most common cause of unexpected directory nesting.

--delete mirrors the destination to match the source exactly. Any file in the destination that does not exist in the source is deleted:

bashrsync -a --delete /src/ user@host:/dest/

Always run a dry run before using --delete. The -n flag prints every file that would be transferred or removed without making any changes:

bashrsync -a -n --delete /src/ user@host:/dest/

Add -v alongside -n for more detail in the dry run output. Running -n first should be a habit before any destructive sync.

Custom Ports, Compression, Progress, and SSH Config

Custom SSH port. When SSH runs on a non-default port, specify it with -e:

bashrsync -a -e "ssh -p 3322" /src/ user@host:/dest/

Compression with -z reduces bandwidth usage on slow or high-latency connections:

bashrsync -a -z /src/ user@host:/dest/

For fast local network transfers or files that are already compressed (zip, jpg, gz), -z adds CPU overhead with little benefit.

Progress and partial transfers. The -P flag combines two behaviors: it shows a live progress bar and keeps partially transferred files so an interrupted transfer can be resumed:

bashrsync -a -P /src/ user@host:/dest/

For very large transfers, run the command inside a screen or tmux session so an SSH disconnection does not abort the transfer.

SSH config file. Host aliases defined in ~/.ssh/config work directly with rsync. If a host named myserver is configured with a custom port and key:

bashrsync -a /src/ myserver:/dest/

rsync inherits the port, key, and username from the config entry automatically.

Use -a for every transfer, check the trailing slash before syncing directories, and always run -n before --delete. For slow connections, add -z. Leave a comment below if you run into any issues.

Cyber Defence

Recent Posts

jobs Command in Linux: List and Manage Background Processes

The jobs command is a Bash builtin that lists all background and suspended processes belonging to the…

1 hour ago

dmesg Command in Linux: Read and Filter Kernel Messages

The Linux kernel writes messages to the kernel ring buffer throughout the boot process and…

1 hour ago

pstree Command in Linux: Visualize Running Process Hierarchies

The pstree command in Linux displays running processes in a tree structure rather than a flat list.…

2 hours ago

type Command in Linux: Show How the Shell Resolves a Name

The type command in Linux shows how the current shell would interpret a name typed on the…

24 hours ago

How to Check Memory Usage in Linux: free, top, and /proc/meminfo

When a Linux system is slow or behaving unexpectedly, memory is one of the first…

24 hours ago

ip Command in Linux: Configure Interfaces, Routes, and ARP

The ip command is the standard network configuration tool on modern Linux systems. It is part of…

24 hours ago