| |

LFCS 118 ๐Ÿง Remote Sync โ€” rsync Basics

โฑ๏ธ Estimated read: 10 min | |

The rsync command is the standard tool for synchronizing files between two locations, local or remote. It is not a simple copy tool. It compares the source and destination, transfers only the differences, and can preserve permissions, timestamps, and symbolic links. The LFCS exam tests the basic syntax, the archive flag, the trailing slash rule, and the dry-run flag. The critical distinction is between rsync as a one-shot copy and rsync as a synchronization tool. A copy without --delete will add and update files but never remove them. A synchronization with --delete makes the destination an exact mirror of the source, including the removal of files that no longer exist on the source.

Key point: rsync -av source/ destination/ copies the contents of source into destination. The trailing slash on the source path is mandatory for this behavior. Without it, rsync copies the directory itself, resulting in destination/source/. The -n or --dry-run flag previews the transfer without making changes. The --delete flag removes files from the destination that are not present in the source.


Why rsync exists and how it differs from scp

The incremental transfer problem. scp copies the entire file every time. A 1 GB file that changed by 1 KB requires a 1 GB transfer. rsync uses a delta-transfer algorithm: it compares the source and destination, identifies the blocks that have changed, and transfers only those blocks. For large files with small changes, this is a dramatic reduction in network usage . The algorithm is the reason rsync is the standard tool for backups and mirroring.

The preservation problem. scp does not preserve permissions, timestamps, or symbolic links by default. A backup copied with scp is a copy of the content, not a copy of the filesystem. rsync with the -a (archive) flag preserves permissions, timestamps, ownership, group, symbolic links, and device files . The -a flag is the shortcut for -rlptgoD, which is the standard set of preservation options .

The synchronization problem. A copy adds and updates files. It does not remove files that were deleted from the source. A backup that grows indefinitely is not a mirror. The --delete flag makes the destination match the source exactly, including the removal of files that no longer exist on the source . This is the mechanism for mirroring, but it is also the most dangerous flag: a mistake in the source path can delete the wrong files on the destination.

The dry-run problem. --delete is dangerous. A typo in the source path can cause the destination to be emptied. The -n or --dry-run flag previews the transfer without making any changes. It shows which files would be transferred, which would be deleted, and which would be skipped. The output is the verification step before the real transfer .

The trade-off. rsync is more complex than scp. The trailing slash rule is a common source of confusion. The --delete flag is destructive. The preservation options have security implications (preserving ownership requires root). But for any transfer that is repeated, large, or needs to be a mirror, rsync is the only correct tool.


a. The basic syntax and the trailing slash rule

The rsync command takes a source and a destination. Either can be local or remote. The remote format is [user@]host:path.

# Copy contents of local directory to a remote directory
rsync -av /home/user/docs/ user@server:/backup/docs/

# Copy contents of a remote directory to a local directory
rsync -av user@server:/backup/docs/ /home/user/docs/

The trailing slash on the source path is the single most important detail. /home/user/docs/ (with slash) means “copy the contents of docs.” /home/user/docs (without slash) means “copy the docs directory itself,” resulting in /backup/docs/docs/ . The destination path does not use a trailing slash to determine behavior; the source path does.

The -a flag is the archive mode. It is equivalent to -rlptgoD: recursive, preserve symlinks, preserve permissions, preserve times, preserve group, preserve owner, preserve devices . The -v flag is verbose. The combination -av is the standard baseline for a backup or mirror.

# Archive mode with verbose output
rsync -av /source/ /destination/

# Archive mode with compression for remote transfer
rsync -avz /source/ user@server:/destination/

The -z flag enables compression during the transfer. It is useful for slow network connections and text files. For already-compressed files (images, videos, archives), the compression adds CPU overhead without reducing the transfer size.


b. Previewing and mirroring with –delete

The --delete flag removes files from the destination that do not exist in the source. It is the difference between a copy and a mirror. The -n flag previews the operation.

# Dry run: preview what would be transferred and deleted
rsync -avn --delete /source/ /destination/

# Real run: mirror the source to the destination
rsync -av --delete /source/ /destination/

The dry-run output shows the files that would be transferred (marked with >f), the files that would be deleted (marked with deleting), and the files that are already up to date. The output is the verification step. If the deletion list contains files that should be preserved, the source path is wrong or the exclusions are missing.

The --exclude flag prevents specific files or directories from being transferred or deleted. The pattern can be a filename, a directory name, or a glob.

# Exclude temporary files and cache directories
rsync -av --delete --exclude='*.tmp' --exclude='.cache/' /source/ /destination/

# Exclude from a file
rsync -av --delete --exclude-from=exclude.txt /source/ /destination/

The --exclude patterns are matched against the relative path within the transfer. A pattern like *.tmp matches any file with the .tmp extension. A pattern like /tmp/ matches only the top-level tmp directory.


c. Remote transfers, compression, and the archive flag

The remote transfer uses SSH by default. The -e flag specifies an alternative remote shell, but the default (ssh) is almost always correct. The -z flag enables compression.

# Push to a remote host
rsync -avz /home/user/docs/ user@server:/backup/docs/

# Pull from a remote host
rsync -avz user@server:/backup/docs/ /home/user/docs/

The -a flag preserves the archive attributes. If the destination is a remote host and the local user is not root, preserving ownership (-o) and devices (-D) will fail silently or produce warnings. The --no-o and --no-g flags disable the ownership and group preservation when they are not needed.

# Preserve permissions and times, but not ownership or devices
rsync -rlptvz /source/ user@server:/dest/

The -P flag combines --partial and --progress. The --partial flag keeps partially transferred files if the transfer is interrupted, allowing rsync to resume from where it stopped. The --progress flag shows the transfer progress.

# Resume interrupted transfers and show progress
rsync -avzP /large-file.iso user@server:/dest/

Complete Example Session

# ============================================
# PART 1: BASIC LOCAL COPY
# ============================================

rsync -av /home/user/docs/ /backup/docs/
# Copies the contents of docs to /backup/docs.
# ============================================
# PART 2: OBSERVE THE TRAILING SLASH
# ============================================

rsync -av /home/user/docs /backup/docs/
# Copies the docs directory itself to /backup/docs/docs/.
# ============================================
# PART 3: DRY RUN WITH DELETE
# ============================================

rsync -avn --delete /home/user/docs/ /backup/docs/
# Shows what would be transferred and deleted.
# ============================================
# PART 4: REAL MIRROR WITH DELETE
# ============================================

rsync -av --delete /home/user/docs/ /backup/docs/
# Deletes files in /backup/docs that are not in /home/user/docs.
# ============================================
# PART 5: EXCLUDE PATTERNS
# ============================================

rsync -av --delete --exclude='*.tmp' --exclude='.cache/' /home/user/ /backup/user/
# Excludes temporary files and cache directories.
# ============================================
# PART 6: REMOTE PUSH
# ============================================

rsync -avz /home/user/docs/ user@server:/backup/docs/
# Compresses during transfer.
# ============================================
# PART 7: REMOTE PULL
# ============================================

rsync -avz user@server:/backup/docs/ /home/user/docs/
# Pulls from the server to the local machine.
# ============================================
# PART 8: RESUME AND PROGRESS
# ============================================

rsync -avzP /large-file.iso user@server:/dest/
# Shows progress and resumes if interrupted.
# ============================================
# PART 9: VERIFY THE TRANSFER
# ============================================

diff -r /home/user/docs/ /backup/docs/
# No output: the directories are identical.

The nine parts trace the rsync workflow: basic copy, trailing slash, dry run, mirror with delete, exclusions, remote push, remote pull, resume/progress, and verification.


Quick Reference

Core Flags

FlagPurpose
-aArchive mode (-rlptgoD)
-vVerbose output
-zCompression
-nDry run
--deleteRemove dest files not in source
--excludeSkip matching files
--exclude-fromRead exclusions from file
-PProgress + resume
-eRemote shell (default ssh)

Archive Components

FlagMeaning
-rRecursive
-lPreserve symlinks
-pPreserve permissions
-tPreserve times
-gPreserve group
-oPreserve owner
-DPreserve devices

Trailing Slash Rule

SourceResult
source/Contents of source in destination
sourcesource directory itself in destination

Best Practices

โœ… Do This:

rsync -av /home/user/ /backup/user/
<!-- Use trailing slash on source -->                # โœ…
rsync -avn --delete /home/user/ /backup/user/
<!-- Always dry-run before --delete -->              # โœ…
rsync -avzP /large.iso user@server:/dest/
<!-- Use -P for large files -->                       # โœ…

โŒ Don’t Do This:

rsync -av /home/user /backup/user/
<!-- Creates /backup/user/user/ -->                  # โŒ
rsync -av --delete /home/user/ /backup/user/
# Without dry run
<!-- Deletes files without preview -->               # โŒ
# Back up /proc, /sys, /dev
<!-- Virtual filesystems cause errors -->            # โŒ

Common Pitfalls

PitfallWhy It HappensFix
Nested directory createdMissing trailing slashAdd / to source
Files deleted unintentionally--delete without dry run-n first
Ownership not preservedRemote user is not rootUse --no-o --no-g
Transfer slowNo compressionAdd -z
Interrupted transfer restartsNo -PAdd -P

Real-World Examples

1. Local Copy

rsync -av /src/ /dest/

2. Mirror with Delete

rsync -av --delete /src/ /dest/

3. Dry Run

rsync -avn --delete /src/ /dest/

4. Remote Push

rsync -avz /src/ user@host:/dest/

5. Remote Pull

rsync -avz user@host:/src/ /dest/

6. Exclude

rsync -av --exclude='*.log' /src/ /dest/

7. Resume

rsync -avzP /big.iso user@host:/dest/

8. Preserve Without Owner

rsync -rlptvz /src/ user@host:/dest/

9. Verify

diff -r /src/ /dest/

10. Cron Backup

0 2 * * * rsync -av --delete /home/ /backup/

Visual

Trailing Slash

โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
โ”‚  Source: /home/user/docs/ (with slash)       โ”‚
โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜
                   โ”‚
                   โ–ผ
โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
โ”‚  Destination: /backup/docs/                  โ”‚
โ”‚  Contents: file1.txt, file2.txt              โ”‚
โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜

โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
โ”‚  Source: /home/user/docs (no slash)          โ”‚
โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜
                   โ”‚
                   โ–ผ
โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
โ”‚  Destination: /backup/docs/                  โ”‚
โ”‚  Contents: docs/file1.txt, docs/file2.txt    โ”‚
โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜

Mirror with –delete

โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
โ”‚  Source: /src/                               โ”‚
โ”‚  โ”œโ”€โ”€ file1.txt                               โ”‚
โ”‚  โ””โ”€โ”€ file2.txt                               โ”‚
โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜
                   โ”‚ rsync -av --delete
                   โ–ผ
โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
โ”‚  Destination: /dest/                         โ”‚
โ”‚  โ”œโ”€โ”€ file1.txt                               โ”‚
โ”‚  โ”œโ”€โ”€ file2.txt                               โ”‚
โ”‚  โ””โ”€โ”€ old-file.txt (deleted)                  โ”‚
โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜

Summary

ItemValue
Syntaxrsync [options] source destination
Remote format[user@]host:path
Archive flag-a (-rlptgoD)
Compression-z
Dry run-n
Delete--delete
Exclude--exclude
Resume-P
Trailing slashSource / copies contents
Default remote shellssh

Key takeaways:

  • rsync transfers only the differences. The delta-transfer algorithm compares the source and destination and transfers only the changed blocks. For large files with small changes, this is a dramatic reduction in network usage .
  • The -a flag is the baseline. It preserves permissions, timestamps, symlinks, and ownership. It is the shortcut for -rlptgoD .
  • The trailing slash rule is mandatory. source/ copies the contents. source copies the directory itself. The destination path does not affect this behavior .
  • --delete is destructive. It removes files from the destination that do not exist in the source. The -n flag must be used first to preview the deletion list.
  • Compression is for remote transfers. The -z flag reduces the transfer size for text files. It adds CPU overhead for already-compressed files.

Remember: rsync is not scp. scp copies a file. rsync synchronizes a destination with a source. The -a flag preserves the filesystem metadata. The trailing slash determines whether the directory or its contents are transferred. The --delete flag makes the destination a mirror. The -n flag is the safety check. The exam tests the syntax, the trailing slash, and the dry-run flag.

Was this helpful?


Stop using slow, ad-bloated tool sites! ๐Ÿคฎ

๐Ÿ”Ž Search “KandZ Tools” on Google to use many professional utilities for free.

KandZ.me is the ultimate minimalist hub for:
โœ… Finance (Mortgage, Interest, Inflation)
โœ… Tech (Base64, JSON, Dev Suite, IP)
โœ… Health (BMI, BMR, TDEE)
โœ… Productivity (Timer, Workspace, QR)

โšก๏ธ Fast & Private
๐Ÿ”’ No data leaves your device
๐Ÿ’Ž 100% Free

๐Ÿ”— Use it now: https://tools.kandz.me
๐Ÿ”– Bookmark itโ€”youโ€™ll need it later!