$_ CLI In Depth
Learning progress 0%
lesson networking sockets ssh

Copying Over SSH: scp & rsync

3 min read Module 16: Networking Tools Updated Dec 5, 2026
Cheat Sheet
0:00 / 3:17

scp copies whole files

The scp command copies files over an encrypted ssh connection. Its syntax puts a host and a colon in front of a remote path, so the source or destination can be on another machine. It copies everything it is told to copy, every time, which makes it simple and predictable for one-off transfers.

TEXT1 lines
scp report.txt devbox:/home/deploy/reports/

This command, which is shown as reference text, copies report.txt to the reports directory on the host devbox. The config alias from the previous unit works here as well. The lab image has no scp, so the line is not run.

rsync sends only changes

The rsync tool compares the source and destination and sends only the differences. It can copy a directory tree and keep permissions, times, and links. The -a flag selects archive mode, which means recursive copy with all metadata preserved, and a trailing slash on the source changes what is copied.

TEXT1 lines
rsync -av ./site/ devbox:/var/www/site/

A second run of the same command sends almost nothing when the files have not changed. The trailing slash on ./site/ means copy the contents of the directory, not the directory itself. Forgetting the slash is a common source of an extra nested directory on the destination.

Warning

rsync --delete removes files on the destination that are missing from the source. Run it with --dry-run first, and read the list it prints before you let it delete anything.

The stream pattern with tar

A copy over a pipe makes the mechanism visible. The tar tool can write an archive to standard output and read one from standard input. Connecting the two with a pipe copies a tree from one directory to another with no network at all. Over ssh, the same pipe is split across two machines, with ssh carrying the bytes between them.

BASH2 lines
mkdir -p dst
tar -C src -cf - . | tar -C dst -xf -

The first tar reads the source directory and writes the archive to standard output, named by the dash. The second tar reads the archive from standard input and extracts it into dst. The result is a copy of the tree. The -C option changes directory before each action, so the paths inside the archive stay relative.

Choosing a tool

Use scp for a single file or a quick copy. Use rsync for repeated copies of a tree, for mirroring, and for large transfers where only changes should move. Use a tar pipe when you want full control of the stream or need a copy that can be inspected in the middle.

Checking the result

After any copy, compare the two sides. The diff command with the -r flag compares directories recursively and prints nothing when they match. Its exit status is 0 for identical trees and 1 when they differ, which lets a script decide.

BASH1 lines
diff -r src dst && echo identical

The check prints identical when the two trees match. The lab asks you to copy a small tree with a tar pipe and to record the result of this comparison.

Terminal lab · 2 steps · real Linux in your browser

Copy a tree with a tar pipe

Copy a directory tree through a pipe, then compare the two trees with diff -r.

01
Copy a tree through a pipe
Create the directory dst in /workspace and copy the tree src into it with a pipe between two tar commands. The first tar reads src and writes an archive to standard output, and the second tar extracts it into dst: ``bash mkdir dst tar -C src -cf - . | tar -C dst -xf - ``
Show hint for step 1
The dash after -f means standard output on the first tar and standard input on the second. The -C option changes into the directory first.
02
Compare the two trees
Compare src and dst with diff -r. If the comparison succeeds, write identical to verify.txt in /workspace. Use the && operator so the file is written only on success: ``bash cd /workspace && diff -r src dst && echo identical > verify.txt ``
Show hint for step 2
diff -r exits with status 0 when the trees match, so the echo runs only then.
Check yourself Question 1 of 4
Concept Check Archive mode in rsync recall

Which rsync flag selects archive mode, which recurses and keeps permissions, times, and links?

Recap & Key Takeaways

scp copies whole files over ssh, and rsync -a copies trees and sends only changes when run again.
A trailing slash on the source in rsync copies the contents of the directory, not the directory itself.
A tar pipe, tar -C src -cf - . | tar -C dst -xf -, shows the same stream idea that scp uses over ssh.
diff -r exits 0 when two trees match, which makes it a simple check after a copy.
Was this lesson helpful? Spot an issue or have a suggestion? Let us know on GitHub.
Report an issue