scp — secure copy — moves files between machines over SSH with the same key authentication and encryption you already use for shells. It is the fastest path from "file here" to "file there", and it earns its keep precisely because there is nothing to set up beyond SSH itself.
The two directions #
$ scp notes.txt ana@build.aospix.com:/tmp/ # push: to remote
$ scp ana@build.aospix.com:/var/log/app.log . # pull: from remote
The colon is the entire syntax: host:path means remote, absence means local. Read each command as source-then-destination and the direction is always obvious. Recursion is -r:
$ scp -r project/ ana@build.aospix.com:/srv/
$ scp -r ana@build.aospix.com:/srv/releases ./
The details that matter #
| Form | Effect |
|---|---|
scp file host: | To your home directory on the remote |
-P 2222 | Non-standard port (capital P — ssh uses lowercase) |
-i ~/.ssh/id_ed25519 | Specific key, same as ssh |
-C | Compress in transit — good for text over slow links |
-p | Preserve times and modes |
host1:file host2:path | Remote to remote through your machine |
The port flag trap is eternal: ssh takes -p, scp takes -P. A ~/.ssh/config entry with Port 2222 makes the difference invisible — both tools read the same config, which is the durable fix.
When to upgrade to rsync #
scp transfers everything you name, every time. On repeated transfers — logs pulled hourly, trees synced during a deploy — rsync over ssh sends only the changed blocks, resuming where it left off:
$ rsync -avP project/ ana@build.aospix.com:/srv/project/
The rule of thumb: one-off file or first sync — scp; anything repeated or interruptible — rsync. Both ride the identical SSH authentication, so the upgrade is free.
Identity and keys #
scp has no authentication of its own; it is ssh. Keys set up once (ssh-keygen, ssh-copy-id) apply to every transfer, and agents forward so you can scp between two remotes without keys on either. When scp asks for a password every time, the fix is the SSH fix, not an scp flag.
Quoting matters the moment remote paths contain spaces: scp "host:'/path/My Folder/f.txt'" . — the inner quotes survive to the remote shell. And before a bulk -r, a dry ssh ls of the destination confirms you are aimed at the right directory.
For mounted-in-place remote work, see sshfs; for HTTP instead of SSH, curl and wget. The network series continues.
