LFCS 118 ๐ง Remote Sync โ rsync Basics
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
| Flag | Purpose |
|---|---|
-a | Archive mode (-rlptgoD) |
-v | Verbose output |
-z | Compression |
-n | Dry run |
--delete | Remove dest files not in source |
--exclude | Skip matching files |
--exclude-from | Read exclusions from file |
-P | Progress + resume |
-e | Remote shell (default ssh) |
Archive Components
| Flag | Meaning |
|---|---|
-r | Recursive |
-l | Preserve symlinks |
-p | Preserve permissions |
-t | Preserve times |
-g | Preserve group |
-o | Preserve owner |
-D | Preserve devices |
Trailing Slash Rule
| Source | Result |
|---|---|
source/ | Contents of source in destination |
source | source 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
| Pitfall | Why It Happens | Fix |
|---|---|---|
| Nested directory created | Missing trailing slash | Add / to source |
| Files deleted unintentionally | --delete without dry run | -n first |
| Ownership not preserved | Remote user is not root | Use --no-o --no-g |
| Transfer slow | No compression | Add -z |
| Interrupted transfer restarts | No -P | Add -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
| Item | Value |
|---|---|
| Syntax | rsync [options] source destination |
| Remote format | [user@]host:path |
| Archive flag | -a (-rlptgoD) |
| Compression | -z |
| Dry run | -n |
| Delete | --delete |
| Exclude | --exclude |
| Resume | -P |
| Trailing slash | Source / copies contents |
| Default remote shell | ssh |
Key takeaways:
rsynctransfers 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
-aflag 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.sourcecopies the directory itself. The destination path does not affect this behavior . --deleteis destructive. It removes files from the destination that do not exist in the source. The-nflag must be used first to preview the deletion list.- Compression is for remote transfers. The
-zflag 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.
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!