Chapter 14Lesson 03~100 minutes

scp, sftp, and rsync Transfer Workflows

Remote file transfer is not one problem. One-shot copies, protocol-level file operations, and repeated tree synchronization have different tools and different failure modes.

IntermediateNetworking & remote automationHands-on lab

Learning objectives

By the end of this lesson

  • Choose between scp, sftp, and rsync.
  • Use non-interactive SSH transfer options.
  • Understand rsync trailing-slash semantics.
  • Preview destructive synchronization.
  • Verify and stage transferred artifacts.

1. Choose the transfer tool by workflow

ToolStrengthBest fit
scpSimple file/directory copy over SSHOne-shot transfer
sftpInteractive/batch file-transfer protocolExplicit remote file operations
rsyncIncremental synchronization over local/remote transportLarge trees, deltas, exclusions

2. scp is simple for one-shot copies

scp \
  -o BatchMode=yes \
  -o ConnectTimeout=5 \
  ./artifact.tar.gz \
  ops@example-host:/srv/releases/

Use the same SSH trust and identity controls you would use with ssh.

3. Remote-path syntax creates another parsing boundary

scp './release candidate.tar.gz' \
  'ops@example-host:/srv/releases/release candidate.tar.gz'

Spaces and metacharacters in remote paths increase quoting complexity. Prefer simple deployment path conventions when you control the remote layout.

4. sftp batch mode makes operations explicit

cat > commands.sftp <<'EOF'
put artifact.tar.gz /srv/releases/artifact.tar.gz
ls -l /srv/releases
EOF

sftp \
  -b commands.sftp \
  -o BatchMode=yes \
  ops@example-host

Batch files are useful when a transfer includes several protocol-level operations.

5. rsync is optimized for synchronization

rsync -a \
  --delete-delay \
  --exclude '.git/' \
  ./site/ \
  ops@example-host:/srv/site/
Direction matters

Options such as --delete can remove remote files. Validate source, destination, and dry-run output before enabling destructive synchronization.

6. rsync dry-run is a first-class safety tool

rsync -anv \
  --delete \
  ./site/ \
  ops@example-host:/srv/site/

Review which files would be created, updated, or deleted before the real operation.

7. Trailing slash semantics matter in rsync

# Copy contents of site into /srv/site:
rsync -a ./site/ host:/srv/site/

# Copy directory site itself into destination:
rsync -a ./site host:/srv/

This small syntax difference changes the resulting directory tree and should be covered by tests or deployment documentation.

8. Partial transfer behavior should be intentional

Large files may benefit from resumable or partial-transfer options, but partially transferred content should not become visible under a final production filename until validation is complete.

9. Transfer success is not content validation

sha256sum artifact.tar.gz > artifact.sha256

scp artifact.tar.gz artifact.sha256 \
  ops@example-host:/srv/staging/

ssh ops@example-host \
  'cd /srv/staging && sha256sum -c artifact.sha256'

End-to-end verification catches corruption and wrong-content mistakes that transport success alone cannot detect.

10. Transfer to staging, then promote

scp artifact.tar.gz host:/srv/staging/artifact.tar.gz

ssh host '
  verify_artifact /srv/staging/artifact.tar.gz &&
  mv /srv/staging/artifact.tar.gz /srv/releases/artifact.tar.gz
'

Remote staging prevents consumers from seeing an incomplete or unverified artifact.

11. Hands-on lab: design an rsync deployment command

mkdir -p "$HOME/devops-academy/bash/chapter14/lesson03/site"
cd "$HOME/devops-academy/bash/chapter14/lesson03"

printf '<h1>hello</h1>\n' > site/index.html
printf 'version=1\n' > site/version.txt

cat > deploy-preview.sh <<'EOF'
#!/usr/bin/env bash
set -u

host=${1:-}
dest=${2:-/tmp/site-preview}

[[ -n $host ]] || {
  printf 'usage: %s HOST [DEST]\n' "$0" >&2
  exit 64
}

rsync \
  -anv \
  --delete \
  --exclude '.git/' \
  -e 'ssh -o BatchMode=yes -o ConnectTimeout=5' \
  ./site/ \
  "$host:$dest/"
EOF

chmod u+x deploy-preview.sh
printf 'Run with a reachable SSH host to preview synchronization.\n'

Verification checklist

12. Knowledge check

Question 1. When is rsync preferable to scp?

Question 2. Why is rsync dry-run important with delete options?

Question 3. Does successful transfer prove the artifact contents are correct?

Question 4. Why use a remote staging directory?

13. Summary

Use scp for straightforward copies, sftp for protocol-level file operations, and rsync for repeated synchronization. Make remote-path semantics explicit, preview destructive changes, verify content, and promote from staging only after validation.

14. Further reading

  • OpenSSH scp(1) and sftp(1) manuals.
  • rsync documentation — archive, delete, dry-run, trailing slashes.
  • OpenSSH security and host-key documentation.
  • GNU Coreutils checksum utilities.
Next lesson

Quoting Across Local and Remote Shell Boundaries

Continue Chapter 14 by making remote networking and automation boundaries safer and more explicit.

Keep the academy open

Support free, practical DevOps education.

Every lesson is designed to remain readable in a browser, downloadable from GitHub, and usable without a paid learning platform. Contributions help expand and maintain the curriculum.

Ethereum / ERC-20
0x716c4Ab160C4B66F31a28AE2448BfF68fc3a2ef0 Send only Ethereum/ERC-20 compatible assets to this address.