27.6 scp: Copying Files Over SSH
Alright, let’s talk about scp. It stands for Secure Copy Protocol, and for years it was the go-to tool for moving files over SSH. It’s simple, it’s ubiquitous, and it gets the job done. You’ll still see it in a million old tutorials and scripts. But before we get into the nitty-gritty, I need to be brutally honest with you: while scp is still perfectly usable, the cool kids (and the security-conscious ones) have largely moved on to rsync over SSH or the modern sftp for interactive sessions. Why? We’ll get to that. But you still need to know scp because it’s everywhere, and sometimes the simplest tool is the right one for a quick job.
The basic syntax is so straightforward it’s almost criminal. You’re essentially just telling it scp [options] source target. The magic is that either the source or the target can be on a remote machine, denoted by user@host:path.
Copying a File to a Remote Server
This is the “I need to get this thing from my laptop to that server” operation.
scp ./local_report.txt alice@server.example.com:/home/alice/reports/
Here’s what just happened: I authenticated as alice on server.example.com (using my SSH key, hopefully, not a password) and plopped the local_report.txt file into her reports directory. Notice the colon (:)—that’s what tells scp, “Hey, the stuff after this is on a different machine.”
Copying a File from a Remote Server
And this is the “I need to grab that log file from the server before it explodes” operation. Just flip the arguments.
scp alice@server.example.com:/var/log/app/error.log ./
Boom. The error.log file is now sitting in your current local directory. See how the remote part always has that user@host: prefix? That’s the pattern. Remember it.
Those Quirks You Absolutely Must Know
scp has… opinions. And they will bite you if you’re not aware of them.
First, when you copy a file to a remote server, the destination you specify is a directory. You’re saying, “Put my file in this folder.” When you copy a file from a remote server, the destination you specify is also, technically, a directory. But if you want to rename the file during the operation, you can just specify a full filename instead.
# Copies the file and renames it to 'archive.log' locally
scp alice@server.example.com:/var/log/app/error.log ./archive.log
Second, and this is a biggie, scp is very literal about paths. Using a relative path on the remote machine is based on the user’s home directory. So if user alice’s home is /home/alice, then this:
scp important.txt alice@server.example.com:reports/
…is identical to this:
scp important.txt alice@server.example.com:/home/alice/reports/
This is a common source of confusion. You think you’re copying to some system-wide /reports directory, but you’re actually copying into a reports folder in your home directory. Always double-check your paths.
Recursive Copying for Directories
Need to upload an entire website directory? The -r (recursive) flag is your friend.
scp -r ./my_website/ alice@server.example.com:/var/www/html/
This will copy the entire my_website directory and all its contents into /var/www/html/. A word of caution: check what’s in that directory first. You don’t want to accidentally copy a 5-gigabyte node_modules folder or a __pycache__ directory over a slow connection. It’s a rite of passage we’ve all suffered through.
The Elephant in the Room: scp vs. Modern Tools
Here’s the part where I tell you why scp is slowly being deprecated in favor of other tools. The main reason is that the SCP protocol itself is, well, kind of janky. It’s old. It’s not great at reporting errors, it can’t resume interrupted transfers, and most damningly, it’s a nightmare for forward-compatibility. The openssh developers themselves have marked it as legacy and recommend using sftp or rsync instead.
rsync, especially with the -a (archive) and -z (compress) flags, is almost always a better choice. It’s faster (only transfers changes, not the whole file), more robust, and gives you better feedback.
# The better way to do a recursive copy
rsync -az ./my_website/ alice@server.example.com:/var/www/html/
So, should you use scp? For a one-off, simple file copy where you just need to move a few megabytes, sure. It’s quick to type and it works. But for any serious, scripted, or large-scale work, do yourself a favor and graduate to rsync. You’ll thank me later. Consider scp a reliable old friend who’s great for a quick coffee, but maybe not for building a new business together.