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.
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.
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.
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.
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.
diff -r src dst && echo identicalThe 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.